查看模型所有一些字段都返回null?

时间:2016-06-22 09:56:12

标签: asp.net-mvc

我有这个问题我真的很喜欢它,我几乎不知道该怎么做,这是我加载模型的行为,并初始化它

  public ActionResult EditCredentials(int id)
    {
        var user = UserManager.FindById(id);
        var userViewModel = new UserViewModel()
        {
            UserName = user.UserName,
            FName = user.FirstName,
            LName = user.LastName,
            BirthDate = user.BirthDate,
            Phonenumber = user.PhoneNumber,
        };

        var allroles = UserRole.Roles;
        var roles = user.Roles;
        var userRoleViewModels = new List<UserRoleViewModel>();
        foreach (var item in allroles)// initialize All of Role view models
        { 
            var newViewModel=new UserRoleViewModel
            {
                Show = item.Show,
                RoleId = item.Id,
                BaseCoding = item.BaseCoding,
                IsActive = item.IsActive,
                RoleName = item.Name

            };

                foreach (var role in roles.Where(role => role.RoleId==item.Id))
                {  //if new Role Exist In userRole table, then apply is checked
                    newViewModel.ApplyPrem = true;
                    newViewModel.UserId = role.UserId;

                }
            userRoleViewModels.Add(newViewModel);
        }
        var userMainViewModel = new UserMainViewModel()// user main view model,  include two model: one "UserViewModel" and a list of "UserRoleViewModel"
        {
            UserViewModel = userViewModel,
            UserRoleViewModel = userRoleViewModels
        };

        return View(userMainViewModel);
    }

你可以看到我的观点是IList这是我的观点

  @model IList<Mfr.Admin.Models.User.UserRoleViewModel>
@using (Html.BeginForm("EditPermissions", "User", FormMethod.Post, new    
 {@class = "form-horizontal"}))
 {
        @for (var i = 0; i < Model.Count; i++)
        {
            <tbody>
                <tr>
                    <td>
                        @*id*@
                        @(Html.DisplayFor(modelItem => modelItem[i].RoleId))
                    </td>
                    <td>
                        @*Apply Perm*@
                        @Html.CheckBoxFor(modelItem => modelItem[i].ApplyPrem)
                    </td>
                    <td>
                        @*name*@
                        @(Html.DisplayFor(modelItem => modelItem[i].RoleName))
                    </td>
                    <td>
                        @*base coding*@
                         @(Html.Kendo().CheckBoxFor(modelItem => modelItem[i].BaseCoding).HtmlAttributes(new { @disabled = "disabled" }))
                    </td>
                    <td>
                        @*show*@
                          @(Html.Kendo().CheckBoxFor(modelItem => modelItem[i].Show).HtmlAttributes(new { @disabled = "disabled" }))
                    </td>
                    <td>
                        @*Active*@
                        @(Html.Kendo().CheckBoxFor(modelItem => modelItem[i].IsActive).HtmlAttributes(new { @disabled = "disabled" }))
                    </td>
                </tr>

            </tbody>
        }
 }
   <input type="submit" value="update" class="btn btn-success" />

这是后期行动的第一行

         public ActionResult EditPermissions(IList<UserRoleViewModel> model)

问题是:当我想发布这个模型时,只修改了applyperm字段并正确返回,并且我的视图模型中的所有其他字段(如userId RoleId和...)都返回表单视图为null? 有谁知道为什么? 这是我的ViewModel定义

    public class UserRoleViewModel
    {
    public bool ApplyPrem { get; set; }
    public int UserId { get; set; }

    public int RoleId { get; set; }

    public string RoleName { get; set; }

    public bool IsActive { get; set; }

    public bool BaseCoding { get; set; }

    public bool Show { get; set; }
}

只有一件事,当模型在视图中初始化并在我的视图中迭代时,它完全加载没有任何问题,但问题是当我想在我的控制器后期操作中发布此函数时开始。 Thanx提前

0 个答案:

没有答案