I am trying to setup a login facility for members on my umbraco site, but am having issues when rendering the view.
Here is some information on my setup:
Umbraco version:
8.6.1
Modelsbuilder mode:
PureLive
Doc Type:
accountLogin
Model:
public class CustomerLogin
{
public CustomerLogin()
{
ViewData = new MembershipViewData();
}
[Required(ErrorMessage = "Username is required.")]
[Display(Name = "User Name")]
public string Username { get; set; }
[Required(ErrorMessage = "Password is required.")]
[Display(Name = "Password"), DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember Me")]
public bool RememberMe { get; set; }
public string SuccessRedirectUrl { get; set; }
public MembershipViewData ViewData { get; set; }
}
Controller
public class AccountLoginController : SurfaceController
{
public ActionResult LoginForm(string view = "")
{
var model = new CustomerLogin { RememberMe = true };
return view.IsNullOrWhiteSpace() ? PartialView(model) : PartialView(view, model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(CustomerLogin model)
{
if (!ModelState.IsValid) return CurrentUmbracoPage();
if (!Members.Login(model.Username, model.Password))
{
var member = Members.GetByUsername(model.Username);
var viewData = new MembershipViewData { Success = false };
if (member == null)
{
viewData.Messages = new[] { "Account does not exist for this email address." };
}
else
{
var messages = new List<string>
{
"Login was unsuccessful with the email address and password entered."
};
if (!member.Value<bool>("umbracoMemberApproved")) messages.Add("This account has not been approved.");
if (member.Value<bool>("umbracoMemberLockedOut")) messages.Add("This account has been locked due to too many unsuccessful login attempts.");
viewData.Messages = messages;
}
TempData["LoginModel"] = viewData;
return CurrentUmbracoPage();
}
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
Layout = "priceBeater.cshtml";
}
<section>
<div class="container">
<h1>Login</h1>
<p>Currently not a member? Go to our <a href="/Register">Registration page</a>.</p>
<div class="row">
<div class="col-md-8">
@Html.Partial("~/Views/Partials/Forms/LoginForm.cshtml")
</div>
</div>
</div>
</section>
Going round in circles here, getting messages like the following:
"Cannot bind source type Umbraco.Web.PublishedModels.AccountLogin to model type PriceBeater.Models.Membership.CustomerLogin. The source is a ModelsBuilder type, but the view model is not. The application is in an unstable state and should be restarted."
I am using the following youtube resource to get this facility set up.
youtube video
Can anyone suggest where I am going wrong? If more information is required please let me know.
Here is my site structure, "logintest" is the page that uses the above doctype. It also uses the template "accountLogin" which the login form partial is rendered within.
Here is my site file structure. (see below) and the code for the model/controller/view are supplied in my post above.
I am receiving the error received in my initial post when just trying to navigate to the "logintest" page.
If there is any further information required please let me know. Any help on this will be much appreciated as cannot seem to grasp where I am going wrong.
Well the issue is not that you are using UmbracoViewPage, but what you are using as the model of that view. You are trying to render a partial with the AccountLogin model, but in the partial view you expect the CustomerLogin.
@Html.Partial("/Views/Partials/Forms/LoginForm.cshtml", new CustomerLogin())
So sorry for the confusion in my answer before. I think that UmbracoViewPage should also work in the partial, but then with the CustomerLogin model. You are gonna have to test that out.
Umbraco v8 ModelsBuilder Issues
I am trying to setup a login facility for members on my umbraco site, but am having issues when rendering the view.
Here is some information on my setup:
Umbraco version: 8.6.1
Modelsbuilder mode: PureLive
Doc Type: accountLogin
Model:
Controller
View: (Partial View)
Template Page
Going round in circles here, getting messages like the following:
"Cannot bind source type Umbraco.Web.PublishedModels.AccountLogin to model type PriceBeater.Models.Membership.CustomerLogin. The source is a ModelsBuilder type, but the view model is not. The application is in an unstable state and should be restarted."
I am using the following youtube resource to get this facility set up. youtube video
Can anyone suggest where I am going wrong? If more information is required please let me know.
Much appreciated.
Hi,
Well this isn't an ModelsBuilder issue probably. Somewhere in your code it is giving an AccountLogin model where you expect a CustomerLogin model.
Can you give more information on what step in the login flow it is giving this error?
-Joep
Here is my "accountLogin" doctype:
Here is my site structure, "logintest" is the page that uses the above doctype. It also uses the template "accountLogin" which the login form partial is rendered within.
Here is my site file structure. (see below) and the code for the model/controller/view are supplied in my post above.
I am receiving the error received in my initial post when just trying to navigate to the "logintest" page.
If there is any further information required please let me know. Any help on this will be much appreciated as cannot seem to grasp where I am going wrong.
Hi,
In your login partial, you should set the @inherits UmbracoViewPage to @model CustomerLogin.
And where you call the partial, you should give a new CustomerLogin().
Let me know how it went!
-Joep
I will give this a go later and let you know. In the meantime why couldn’t I use @inherits UmbracoViewPage
Please can you explain this umbracoviewpage and why it won’t work in this case?
Much appreciated by the way in your fast response!
Hi,
Well the issue is not that you are using UmbracoViewPage, but what you are using as the model of that view. You are trying to render a partial with the AccountLogin model, but in the partial view you expect the CustomerLogin.
So sorry for the confusion in my answer before. I think that UmbracoViewPage should also work in the partial, but then with the CustomerLogin model. You are gonna have to test that out.
-Joep
Thanks for this Joep. The changes you suggested has worked a treat. Much appreciated!
is working on a reply...