【问题标题】:Pass model value from View to controller is not working将模型值从视图传递到控制器不起作用
【发布时间】:2013-02-03 17:12:15
【问题描述】:

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

  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;    

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

【问题讨论】:

  • 您的帖子或等效按钮在哪里?

标签: asp.net asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

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

@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>
}

【讨论】:

    【解决方案2】:

    我认为 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);
            ....
        }
     }
    

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 2013-01-17
      相关资源
      最近更新 更多