0
votes

I am trying to create a custom login form with Umbraco 7.6.2 I created a partial view for the form, a model and a surface controller based on Umbraco.Web.Mvc.SurfaceController.

After all that I get the error:

Cannot bind source type 'name of my model' to model type Umbraco.Core.Models.IPublishedContent.

Controller:

public class UserSurfaceController : Umbraco.Web.Mvc.SurfaceController
{
    //
    // GET: /User/
    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(Models.UserModel user)
    {
        if (ModelState.IsValid)
        {
            if (user.IsValid(user.UserName, user.Password))
            {
                FormsAuthentication.SetAuthCookie(user.UserName, user.RememberMe);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect!");
            }
        }
        return View(user);
    }
    public ActionResult Logout()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }
}

Model:

public class UserModel 
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember on this computer")]
    public bool RememberMe { get; set; }

    /// <summary>
    /// Checks if user with given password exists in the database
    /// </summary>
    /// <param name="_username">User name</param>
    /// <param name="_password">User password</param>
    /// <returns>True if user exist and password is correct</returns>
    public bool IsValid(string _username, string _password)
    {
        if (_username == "username" && _password == "password")
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}

View:

@inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>  
@{
    Layout = "Master.cshtml";
}
@Html.Partial("User", new namespace.UserModel());

Partial view:

@model namespace.UserModel

<div role="content">
<div class="container">
    <div class="row">
        <div class="col">
            @using (Html.BeginUmbracoForm<namespace.Controllers.UserSurfaceController>("Login", "UserSurface"))
            {
                <div>
                    <fieldset>
                        <legend>Login</legend>
                        <div class="editor-label">
                            @Html.LabelFor(u => u.UserName)
                        </div>
                        <div class="editor-field">
                            @Html.TextBoxFor(u => u.UserName)
                            @Html.ValidationMessageFor(u => u.UserName)
                        </div>
                        <div class="editor-label">
                            @Html.LabelFor(u => u.Password)
                        </div>
                        <div class="editor-field">
                            @Html.PasswordFor(u => u.Password)
                            @Html.ValidationMessageFor(u => u.Password)
                        </div>
                        <div class="editor-label">
                            @Html.CheckBoxFor(u => u.RememberMe)
                            @Html.LabelFor(u => u.RememberMe)
                        </div>
                        <input type="submit" value="Log In" />
                    </fieldset>
                </div>
            }
            </div>
    </div>
</div>
</div>

What may I be doing wrong here? Considering this is Umbraco 7.6.2.

1

1 Answers

1
votes

In your View, you are expecting IPublishedContent.

@inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent> 

You didn't provide code of that model. Double check if this model is inheriting RenderModel. Your UserModel for partial is fine.