我有一个复杂的实体用户:
public class User : BaseEntity
{
public virtual Taxi Taxi { get; set; } --> That is why i call it "complex"
public virtual string Login { get; set; }
public virtual string Password { get; set; }
}
其中 Taxi 是 User 的父级(Taxi has-many Users):
public class Taxi : BaseEntity
{
public virtual string Name { get; set; }
public virtual string ClientIp { get; set; }
}
BaseEntity 由 public virtual int Id { get; 私人套装;}
尝试编辑用户时出现问题
[Authorize]
public ActionResult ChangeAccountInfo()
{
var user = UserRepository.GetUser(User.Identity.Name);
return View(user);
}
我的 ChangeAccountInfo.aspx
<fieldset>
<legend>Fields</legend>
<% %>
<div class="editor-label">
<%: Html.LabelFor(model => model.Login) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Login) %>
<%: Html.ValidationMessageFor(model => model.Login) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Password) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Password) %>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<div class="editor-field">
<%: Html.HiddenFor(model => model.Taxi.Name)%>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
发布更改:
[Authorize]
[HttpPost]
public ActionResult ChangeAccountInfo(User model)
{
if (ModelState.IsValid)
{
UserRepository.UpdateUser(model);
return RedirectToAction("ChangeAccountInfoSuccess", "Account");
}
return View(model);
}
但是,(用户模型)参数有 User.Id == 0 --> 用户实体在编辑之前有 5 个
User.Login == "my new login" User.Password
== "my new password"
User.Taxi.Id = = 0 --> User.Taxi 实体在编辑前有 3 个
User.Taxi.Name == "old hidden name"
User.Taxi.ClientIp == null --> User 实体在编辑前有 192.168.0.1
问: 是否可以不使用标签“隐藏”标记实体的所有字段(应该在我的 UpdateUser 中)但在我的 HttpPost 方法中仍然保持不变?例如不是 User.Taxi.ClientIp = null,而是 User.Taxi.ClientIp = 192.168.0.1
我正在使用 nhibernate,如果它重要的话。