我正在尝试使用Umbraco 7.6.2创建自定义登录表单。我创建了基于Umbraco.Web.Mvc.SurfaceController的表单,模型和表面控制器的局部视图。
毕竟我得到了错误:
无法绑定来源类型'我的模型的名称'模型类型Umbraco.Core.Models.IPublishedContent 。
控制器:
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。
答案 0 :(得分:1)
在您的视图中,您期待IPublishedContent
。
@inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
您没有提供该模型的代码。仔细检查此模型是否继承RenderModel
。你的部分UserModel
很好。