簡體   English   中英

mvc4控制器編輯發布操作未填充模型

[英]mvc4 controller Edit Post Action not filling model

我有一個MVC4應用程序,該應用程序使用EntityFramework(而不是首先編寫代碼)針對SQL Server 2008 R2數據庫。

我有一個視圖模型,該模型在控制器的Edit方法中傳遞給View()。

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

當我嘗試保存編輯時,視圖模型具有空的User和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>

瀏覽器正在緩存結果-當我刷新瀏覽器緩存時,它起作用了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM