1

我的模型有两个属性,其中一个是另一个类的对象

  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;    

它们的值为空,但如果我注释第一行,则角色变量有一个值。所以感觉问题与用户属性有关,但我不知道如何解决。

4

2 回答 2

3

您似乎为角色类型使用了一些隐藏字段:

@Html.HiddenFor(model => model.Role, new { id = "hiddenRole" })

但是 Role 属性是一个复杂的类型,你不能将它序列化为隐藏字段。您必须为要发送的每个属性使用隐藏字段:

@Html.HiddenFor(model => model.Role.Property1)
@Html.HiddenFor(model => model.Role.Property2)
@Html.HiddenFor(model => model.Role.Property...)

此外,您似乎在表单中使用了一些已命名的单选按钮,user-role但您的 Role 类上不能有一个具有此名称的属性(属性名称中不能有破折号),所以我想您必须使用正确的名称如果您希望将这些单选按钮的值绑定到关联模型上 Role 类的某些属性,请在此处。

例如,假设您的 Role 类如下所示:

public class Role
{
    public string Value { get; set; }
}

现在您的视图可能如下所示:

@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>
        <ul id="user-roles">
            <li>
                @Html.RadioButtonFor(model => model.Role.Value, "01", new { id = "role-admin" }) 
                Read only
            </li>
            <li>
                @Html.RadioButtonFor(model => model.Role.Value, "02", new { id = "role-member" }) 
                Restricted
            </li>
            <li>
                @Html.RadioButtonFor(model => model.Role.Value, "03", new { id = "role-read" }) 
                Standard
            </li>
            <li>
                @Html.RadioButtonFor(model => model.Role.Value, "04", new { id = "role-subscriber" }) 
                Administrator
            </li>
        </ul>
    </div>

    <button type="submit">OK</button>
}
于 2013-02-03T17:15:42.867 回答
1

我认为 ModelBinder 不会绑定子对象。您可以创建一个自定义 ModelBinder 来绑定您的 Association 类,或者只创建一个 ViewModel 类,将您当前的模型扁平化为单个类。因此,您的 AssociationViewModel 模型类可能如下所示:

public class AssociationViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string RoleName { get; set; }
}


public ActionResult AddUser(AssociationViewModel associationViewModel)
{
    if (ModelState.IsValid)
    { 
        var association = new Association
        {
            User.FirstName = associationViewModel.FirstName,
            Role = new Role { Name = associationViewModel.RoleName }
        };

        IRepository<Association> associationRepository = new IRepository<Association>(db);
        ....
    }
 }
于 2013-02-03T17:29:18.827 回答