在MVC4中绑定ViewModel时出错?

时间:2015-03-04 07:52:34

标签: c# asp.net-mvc-4 data-binding viewmodel

如何在MVC4中更新viewmodel?

控制器

  [HttpPost]
    public ActionResult Create(userViewModel model)
    {           
        var menuIds = string.Join(",", model.MenuIds);
        model.User.MENU = menuIds;
        TryUpdateModel(model);
        if (ModelState.IsValid)
        {               
            _db.Entry(model.User).State = EntityState.Modified;
            _db.SaveChanges();
        }
        return RedirectToAction("Create");
    }

每当我检查模型状态时,我都会User.Menu as null。但menuIds包含值。

查看

  @using MvcSIMS.Models.ViewModels;
  @model userViewModel

   <div class="form-group">
        @Html.LabelFor(model=>model.User.USERNAME, new { @class = "col-sm-2 control-label " })
        <div class="col-sm-6 ">

            @Html.TextBoxFor(model=>model.User.USERNAME, new { @class = "form-control" })
            @Html.ValidationMessageFor(model=>model.User.USERNAME)
        </div>
    </div> 
    <div class="form-group">
        @Html.Label("Menu", new { @class = "col-sm-2 control-label " })
        <div class="col-sm-10 ">
             @*@Html.ListBox("menuIds",Model.MenuList,new { @class = "chosen-select",multiple="multiple",style="width:350px"})*@
            @Html.ListBoxFor(model=>model.MenuIds,Model.MenuList,new {id="menuListBox", @class = "chosen-select",multiple="multiple",
            style="width:350px"})
            @Html.ValidationMessageFor(model =>model.MenuIds)
        </div>

    </div>
    <div class="col-lg-12">
        <button type="submit" id="btnSubmit" class="btn btn-default">
            Submit
        </button>
    </div>

视图模型

    public User User { get; set; }

    [DataType(DataType.Password)]
    [Required(ErrorMessage = "CONFIRM PASSWORD is required")]
    [Display(Name="CONFIRM PASSWORD")]      
    [Compare("PASSWORD",ErrorMessage="Password didnot match")]
    public string ComparePassword { get; set; }

    public IEnumerable<Designation> DesignationList { get; set; }
    public SelectList MenuList { get; set; }

    [Required(ErrorMessage="Select some menu items")]
    public List<string> MenuIds { get; set; }

我认为更新模型时出现了一些问题。

模型

  public partial class User
{

    public int ID { get; set; }

    [Required(ErrorMessage="Select Designation")]
    public Nullable<int> DESIGNATIONID { get; set; }

    [Required(ErrorMessage="UserName is required")] 
    [Remote("IsUserNameAvailable","User",ErrorMessage="UserName already exists.")]
    [Display(Name="USERNAME")]
    public string USERNAME { get; set; }

    [Required(ErrorMessage="Password is required")]
    [DataType(DataType.Password)]
    public string PASSWORD { get; set; }

    public string ADDRESS { get; set; }
    public string PHONE { get; set; }

    [Required(ErrorMessage="Select Menu list")]
    public string MENU { get; set; }

    public Nullable<int> ROLE { get; set; }
    public Nullable<bool> STATUS { get; set; }

    public virtual Designation Designation { get; set; }
    public virtual ICollection<Mobilization> Mobilizations { get; set; }
}

2 个答案:

答案 0 :(得分:0)

你能这样试试吗?

    [HttpPost]
    public ActionResult Create(userViewModel model)
    {           
        var menuIds = string.Join(",", model.MenuIds);
        var dbModel = new userViewModel();
        dbModel.User = new User();
        dbModel.User.MENU = menuIds;

        TryUpdateModel(dbModel);
        if (ModelState.IsValid)
        {               
            _db.Entry(dbModel.User).State = EntityState.Modified;
            _db.SaveChanges();
        }
        return RedirectToAction("Create");
    }

答案 1 :(得分:0)

首先,您需要确保model包含MenuIds的值。 String.Join使用每个元素之间的指定分隔符连接字符串数组的指定元素。如果您的model.MenuIds不是数组,则将其转换为数组。还要检查model.MenuIds中是否有内部对象。如果有内部对象,则在加入期间指定。

var menuIds = String.Join(",", model.MenuIds, 0, model.MenuIds.length );

或者

var menuIds = String.Join(",", model.MenuIds.toArray(), 0, model.MenuIds.length );

另外请确保 - menuIds的最大长度不超过User.MENU