Umbraco 6 MVC - CurrentUmbracoPage() & Model MisMatch
Hello all, I am currently getting a YSOD when trying to use CurrentUmbracoPage() in my SurfaceController.
The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'Site.Models.LoginViewModel'.
I have provided the code of my views & ActionResult in the controller below. I would love to see what you think.
////// Handles the login form when user posts the form/attempts to login//////"model">///
[HttpPost]
publicActionResult HandleLogin(LoginViewModel model)
{
if (!ModelState.IsValid)
{
//return RedirectToCurrentUmbracoPage();return PartialView("Login", model);
}
//Member already logged in - redirect to homeif (Member.IsLoggedOn())
{
returnnewRedirectResult("/");
}
//Lets TRY to log the user intry
{
//Try and login the user...if (Membership.ValidateUser(model.EmailAddress, model.Password))
{
//Valid credentials//Get the member from their email addressvar checkMember = Member.GetMemberFromEmail(model.EmailAddress);
//Check the member existsif (checkMember != null)
{
//Let's check they have verified their email addressif (Convert.ToBoolean(checkMember.getProperty("hasVerifiedEmail").Value))
{
//Update number of logins counterint noLogins = 0;
if (int.TryParse(checkMember.getProperty("numberOfLogins").Value.ToString(), out noLogins))
{
//Managed to parse it to a number//Don't need to do anything as we have default value of 0
}
//Update the counter
checkMember.getProperty("numberOfLogins").Value = noLogins + 1;
//Update label with last login date to now
checkMember.getProperty("lastLoggedIn").Value = DateTime.Now.ToString("dd/MM/yyyy @ HH:mm:ss");
//Update label with last logged in IP address & Host Namestring hostName = Dns.GetHostName();
string clientIPAddress = Dns.GetHostAddresses(hostName).GetValue(0).ToString();
checkMember.getProperty("hostNameOfLastLogin").Value = hostName;
checkMember.getProperty("IPofLastLogin").Value = clientIPAddress;
//Save the details
checkMember.Save();
//If they have verified then lets log them in//Set Auth cookieFormsAuthentication.SetAuthCookie(model.EmailAddress, true);
//Once logged in - redirect them back to the return URLreturnnewRedirectResult(model.ReturnUrl);
}
else
{
//User has not verified their email yet
ModelState.AddModelError("LoginForm.", "Email account has not been verified");
return CurrentUmbracoPage();
//return PartialView("Login", model);
}
}
}
else
{
ModelState.AddModelError("LoginForm.", "Invalid details");
return CurrentUmbracoPage();
//return PartialView("Login", model);
}
}
catch (Exception ex)
{
ModelState.AddModelError("LoginForm.", "Error: " + ex.ToString());
return CurrentUmbracoPage();
//return PartialView("Login", model);
}
//Update success flag
TempData["IsSuccessful"] = true;
return RedirectToCurrentUmbracoPage();
}
This error is not because of the code above, it is because of the code you left out ;)
This error is saying that you are trying to render a View that has a model of type "LoginViewModel" but you are passing it a model of type "RenderModel". The view that has this model is "Login.cshtml". The code that you are using to render this view is
@Html.Action("RenderLogin","AuthSurface")
So, what is the code for your RenderLogin action?
Also I've noticed the numerous calls for returning a PartialView from your [HttpPost]. So many other people are attempting to do this because they are not actually understanding what is going on. I've posted a similar description of the whole process here (see answer)
@Warren, $10 bucks says you have another .cshtml file called "Login.cshtml" in your ~/Views folder which is a non-partial Umbraco view ?
If that is the case,the problem is because the MVC View Engines are looking in ~/Views for your partial view before it is looking in the place where you've stored your Login.cshtml partial view (most likely in ~/Views/Partials ? )
You have a couple of options:
use a full virtual path to render your partial view: return PartialView("~/Views/Partials/Login.cshtml")
put your partial view inside a controller folder that matches the controller name you are rendering your view with. In this case, you are rendering this partial view using the AuthSurfaceController so you can put your partial view here: ~/Views/AuthSurface/Login.cshtml . Hopefully the view engines will look in controller specific folders first before the generic views or views/partials folder (they should, if not it is a bug)
rename your partial Login.cshtml file to something that doesn't overlap with a non-partial view in your views folder. Like: LoginPartial.cshtml
I have a Login.chstml for the main template in the /Views folder and then I have the Login.chstml view in the /Views/AuthSurface folder. I will try at lunchtime today to see if providing the full paths will resolve this issue, but I am sure it will.
@Shannon - Thank you for the solution. I have spent about three hours this morning trying to figure out what I was doing wrong. I had a similar setup as Warren (using the CWS Membership bits as a guide). I am so glad I finally found this!
Glad I found this thread - I just spent 2 hours trying to work out what was going on.
Was this ever fixed? I'm using v6.2
put your partial view inside a controller folder that matches the controller name you are rendering your view with. In this case, you are rendering this partial view using the AuthSurfaceController so you can put your partial view here: ~/Views/AuthSurface/Login.cshtml . Hopefully the view engines will look in controller specific folders first before the generic views or views/partials folder (they should, if not it is a bug)
Umbraco 6 MVC - CurrentUmbracoPage() & Model MisMatch
Hello all,
I am currently getting a YSOD when trying to use CurrentUmbracoPage() in my SurfaceController.
The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'Site.Models.LoginViewModel'.
I have provided the code of my views & ActionResult in the controller below.
I would love to see what you think.
Cheers,
Warren
Login Template - Login.cshtml
Login Action Result in SurfaceController
Partial View - Login.cshtml
This error is not because of the code above, it is because of the code you left out ;)
This error is saying that you are trying to render a View that has a model of type "LoginViewModel" but you are passing it a model of type "RenderModel". The view that has this model is "Login.cshtml". The code that you are using to render this view is
@Html.Action("RenderLogin","AuthSurface")
So, what is the code for your RenderLogin action?
Also I've noticed the numerous calls for returning a PartialView from your [HttpPost]. So many other people are attempting to do this because they are not actually understanding what is going on. I've posted a similar description of the whole process here (see answer)
http://our.umbraco.org/forum/developers/api-questions/41357-return-CurrentUmbracoPage()-results-in-Error-loading-Partial-View-script
I'll update the docs to make it clear what the sequence of events is during all of this rendering .
BTW, I've updated the docs here to explain the routing process:
http://our.umbraco.org/documentation/Reference/Mvc/forms#UnderstandingtheRoutingProcess
and added docs describing how to access the ViewData in child actions here:
http://our.umbraco.org/documentation/Reference/Mvc/forms/turorial-child-action#AccessingViewData
Hey Shannon,
Here is my code for my RenderLogin Action
Cheers,
Warren
@Warren, $10 bucks says you have another .cshtml file called "Login.cshtml" in your ~/Views folder which is a non-partial Umbraco view ?
If that is the case,the problem is because the MVC View Engines are looking in ~/Views for your partial view before it is looking in the place where you've stored your Login.cshtml partial view (most likely in ~/Views/Partials ? )
You have a couple of options:
Hey Shan,
Seems like your bang on the money!
I have a Login.chstml for the main template in the /Views folder and then I have the Login.chstml view in the /Views/AuthSurface folder.
I will try at lunchtime today to see if providing the full paths will resolve this issue, but I am sure it will.
Cheers,
Warren :)
@Shannon - Thank you for the solution. I have spent about three hours this morning trying to figure out what I was doing wrong. I had a similar setup as Warren (using the CWS Membership bits as a guide). I am so glad I finally found this!
Justin
@Shannon - Where do we send the $10 ! Spot on thank you.
Kyle
Glad I found this thread - I just spent 2 hours trying to work out what was going on.
Was this ever fixed? I'm using v6.2
is working on a reply...