Asp.net mvc ASP.NET MVC 2编辑控制器不';不能使用viewmodel
嘿,问题是ASP.NET MVC 2中的编辑控制器不起作用。我尝试了很多方法,但都不管用 下面是一个示例代码:Asp.net mvc ASP.NET MVC 2编辑控制器不';不能使用viewmodel,asp.net-mvc,controller,Asp.net Mvc,Controller,嘿,问题是ASP.NET MVC 2中的编辑控制器不起作用。我尝试了很多方法,但都不管用 下面是一个示例代码: [Authorize] public ActionResult Edit() { var edit = new UserViewModel { User = Database.Users.Single(u => u.UserName == User.Identity.Name) }; return View(edit); } [Auth
[Authorize]
public ActionResult Edit() {
var edit = new UserViewModel {
User = Database.Users.Single(u => u.UserName == User.Identity.Name)
};
return View(edit);
}
[Authorize]
[HttpPost]
public ActionResult Edit(FormCollection formCollection) {
var edit = new UserViewModel {
User = Database.Users.Single(u => u.UserName == User.Identity.Name)
};
// TODO: try, catch
UpdateModel(edit, "User");
Database.SaveChanges();
return View(edit);
}
下面是一个视图模型类:
public class UserViewModel {
public User User { get; set; }
}
我应该如何将此用户模型更新到数据库?A当我仅使用电子邮件字段时:
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<div>
<div class="UserFieldLeft"><%: Html.LabelFor(model => model.User.Email) %></div>
<div class="UserFieldRight"><%: Html.TextBoxFor(model => model.User.Email, new { style="width: 200px" }) %></div>
<div class="UserFieldHelper"><%: Html.ValidationMessageFor(model => model.User.Email) %></div>
<p><input class="UserFieldInput" type="submit" value="Zmień email" /></p>
</div>
<% } %>
model.User.Email)%%>
model.User.Email,新的{style=“width:200px”})%>
model.User.Email)%%>
如果我使用本机用户模型,它就不起作用了。发生了什么?我在哪里犯了错误
顺便说一下,我必须使用视图模型(将来)向我的用户添加一些复选框(头发颜色、长度等)
感谢您的时间和帮助。您不需要前缀“User”
应该有用。在formsCollection中,它们应该是User.Email的密钥。这应该映射到用户对象中的Email属性。我这样做了,发现了一个错误无法更新“Project.ViewModels.UserViewModel”类型的模型。我真的不明白怎么了。我已经阅读了一些教程,这种方法效果很好。我认为问题在于你的用户对象。这是什么东西?您可以尝试UpdateModel(edit.User,“User”,新字符串[]{“Email”});只更新用户的电子邮件属性。我想你是对的。我刚刚创建了一个新的MVC2应用程序,没有任何提供程序或验证程序。效果很好。用户对象取自SQL Server 2008。它使用默认成员类编译用户字段。问题在于部分用户类-用户\u验证。现在它更新了一个模型,但不验证数据。我很困惑。我建议你问一个关于stackoverflow的新问题,包括用户和用户验证的细节
UpdateModel(edit);