Return partial view from page controller

ZZ
ZZ
Vote:
 

Hi,

   We are in process of migrating from EPiServer to Optimizely 12 (.NET 6)., and have encountered this issue of partial view.

Basically we have a component (login block) and in the razor view of the loginblock we are calling another partial view ->

                                @await Html.PartialAsync("_Login", Model.Login)

Which shows the form modal with two input fields (username & password) for login. The post request (Ajax form) is sent to our start page which contains a post action method   

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(Login vm)
        {
            if (ModelState.IsValid)
                return await LoginMethod(vm);
            return PartialView("_Login",vm);
        }

If modelstate is not valid it should return the partial view, what it does is just return the partialview markup and nothing else. like this 

In EPiServer 11 it was returning whole page layout with bootstrap etc., and the partial view was embedded into it.

Any help would be appreciated

#293493
Dec 21, 2022 16:09
Vote:
 

Can you try using 

@await Component.InvokeAsync("_Login")
#293496
Dec 21, 2022 17:22
ZZ
Vote:
 

Hi Minesh,

Thanks for your input. In page controller I can't write @await Component.InvokeAsync("_Login") but I tried to write 

            return ViewComponent("_Login");

and I got the error that viiew component _Login couldn't be found and it makes sense as _Login is not a view component but just partial view 

InvalidOperationException: A view component named '_Login' could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with 'ViewComponentAttribute' or have a class name ending with the 'ViewComponent' suffix. A view component must not be decorated with 'NonViewComponentAttribute'.

#293498
Dec 21, 2022 19:10
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.