我的模型有两个属性,其中一个是另一个类的对象
public class Association : Entity
{
public Association()
{
this.User = new User();
}
public User User
{
get;
set;
}
public Role Role
{
get;
set;
}
};
我的观点是强类型到这个模型
@model MuddyBoots.Greenlight.Association
.
.
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div>
@Html.TextBoxFor(model => model.User.FirstName,new { id = "first-name" })
<span class="red-asterisk">@Html.ValidationMessageFor(model => model.User.FirstName)</span>
</div>
<div>
@Html.HiddenFor(model => model.Role, new { id="hiddenRole"})
<ul id="user-roles">
<li><input type="radio" name="user-role" id="role-admin" value="01" checked="checked" /> Read only</li>
<li><input type="radio" name="user-role" id="role-member" value="02" /> Restricted</li>
<li><input type="radio" name="user-role" id="role-read" value="03"/> Standard</li>
<li><input type="radio" name="user-role" id="role-subscriber" value="04" /> Administrator</li>
</ul>
</div>
}
我的控制器函数是这样写的:
[HttpPost]
public ActionResult AddUser(Association association)
{
string firstName = association.User.FirstName;
var role = association.Role;
IRepository<Association> associationRepository = new IRepository<Association>(db);
if (ModelState.IsValid)
{
siteRepository.Update(site);
return RedirectToAction("Index");
}
return View(association);
}
我的问题是:当我发布我的视图时,我的关联对象为空,它没有值。
更准确地说,当我尝试调试这两行时:
string firstName = association.User.FirstName;
var role = association.Role;
它们的值为空,但如果我注释第一行,则角色变量有一个值。所以感觉问题与用户属性有关,但我不知道如何解决。