mvc4控制器编辑后期操作未填充模型

时间:2014-01-28 23:02:15

标签: c# entity-framework asp.net-mvc-4

我有一个使用EntityFramework(而不是代码优先)的SQL Server 2008 R2数据库的MVC4应用程序。

我有一个视图模型,我在控制器的Edit方法中传递给View()。

public class UserViewModel
{
    public User User { get; set; }
    public int RoleId { get; set; }
    public List<UserAccount> UserAccounts { get; set; }        
}

当我尝试保存编辑时,视图模型的用户和UserAccounts为空,RoleId为0。

没有抛出任何错误 - 关于它为什么不能正确填充的任何想法?

这是我的UserController.Edit动作

public ActionResult Edit(int id = 0)
        {
            UserViewModel vm = new UserViewModel();
            vm.User=_db.FindUserById(id);
            webpages_Roles role = _db.GetRoleByUserId(id);
            vm.RoleId = role.RoleId;
            vm.UserAccounts = _db.GetUserAccountsByUserId(id);
            if (vm.User == null)
            {
                return HttpNotFound();
            }
           return View(vm);
        }

 [HttpPost]
        public ActionResult Edit(UserViewModel vm)
        {
            if (ModelState.IsValid)
            {
                _db.SaveUser(vm.User);
                return RedirectToAction("Index");
            }
            return View(vm);
        }

这是视图

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)    

    @Html.HiddenFor(model => model.User.Id)
    @Html.HiddenFor(model => model.User.UserTypeId)

    <table>
        <tr>
            <td>
                Login
            </td>
            <td>
                @Html.EditorFor(model => model.User.Login)
            </td>
            <td>
                Email
            </td>
            <td>
                @Html.EditorFor(model => model.User.Email)
            </td>
            <td>
                &nbsp;
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td>
                First Name
            </td>
            <td>
                @Html.EditorFor(model => model.User.FirstName)
            </td>
            <td>
                Middle Name
            </td>
            <td>
                @Html.EditorFor(model => model.User.MiddleName)
            </td>
            <td>
                Last Name
            </td>
            <td>
                @Html.EditorFor(model => model.User.LastName)
            </td>
        </tr>
        <tr>
            <td>
                Role
            </td>
            <td>
                @Html.DropDownListFor(m=>m.RoleId,(IEnumerable<SelectListItem>)ViewBag.RolesList, new { onchange = "adjustInterface()" })
                @Html.HiddenFor(m=>m.RoleId)
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
    </table>

    <p>
    </p>

    <select id="cboLocationType" onchange="adjustInterface();">
        <option value="0">User Should Have Access To All Practices For Vendor</option>
        <option value="1">User Should Have Access To Specific Practices For Vendor</option>
    </select>
    <div id="accountSection">
        <h3>
            Accounts</h3>
        <span>
            <input type="text" style="width: 400px" />
            <button>Add</button>
        </span>
        <p />
        <table>
            <tr>
                <td>
                    Practice #1
                </td>
                <td>
                    <button>
                        Remove</button>
                </td>
            </tr>
            <tr>
                <td>
                    Practice #2
                </td>
                <td>
                    <button>
                        Remove</button>
                </td>
            </tr>
        </table>
    </div>    
    <p>
        <input type="submit" value="Save" />
    </p>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

1 个答案:

答案 0 :(得分:0)

浏览器是缓存结果 - 当我刷新浏览器缓存时,它工作