Umbraco 7.6.2:无法将源类型<我的模型>绑定到模型类型 Umbraco.Core.Models.IPublishedContent



我正在尝试使用umbraco 7.6.2创建一个自定义登录表单,我为form,型号,模型和一个基于umbraco.web.mvc.mvc.surfacecontroller创建了部分视图。

毕竟我得到了错误:

无法将源类型"我的模型的名称"绑定到模型类型umbraco.core.models.ipublycontent

控制器:

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");
    }
}

模型:

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;
        }
    }
}

查看:

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

部分视图:

@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>

我在这里做错了什么?考虑到这是Umbraco 7.6.2。

在您的视图中,您期望IPublishedContent

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

您没有提供该模型的代码。仔细检查此模型是否继承了RenderModel。您的 UserModel for部分很好。

相关内容

  • 没有找到相关文章

最新更新