簡體   English   中英

MVC模型作為null傳遞給視圖

[英]MVC model is being passed as null to the view

當正在接收一個不為null的Model時,由於在表中填充了數據,當在下面單擊提交按鈕以觸發下面的Controller方法時,下面的視圖如下所示:空值。

我究竟做錯了什么? 我已經嘗試解決了幾個小時,為什么會這樣呢?為什么我不能將模型傳遞給控制器​​方法,因為我100%確信模型不為null?

控制器方式:

    [HttpPost]
    public ActionResult AssignRole(PageDetailsModel model)
    {
        //controller method here which is not being executed since model being passed is null
    }

視圖:

    @model OnlineLearningLTD.Models.PageDetailsModel

    @{
            ViewBag.Title = "Page Details";
    }

    <h2>Page Details</h2>

    <table class="table table-hover">
    @using (Html.BeginForm("AssignRole", "Page", FormMethod.Post))
    {
        if (Model.CurrPage != null)
        {

            <tr>
                <td>Name</td><td>@Model.CurrPage.Name</td>
            </tr>
            <tr>
                <td>Url</td><td>@Model.CurrPage.Url</td>
            </tr> 
            <tr>
                <td>Assigned Roles:</td>
                @foreach(var role in @Model.CurrPage.Role)
                {
                    <td>@role.Name</td>
                }
            </tr>     

            <tr>
                <td colspan="2"><h3>Assign Role</h3></td>
            </tr>
            <tr>
                <td>Choose Role</td>
                <td>
                 @Html.DropDownListFor(model => model.RoleId, Model.RoleList)

                </td>
            </tr>       
            <tr>
                <td colspan="2" align="right">
                     @if(Model.RoleList.Count() > 0)
                    {
                        <input type="submit" value="Assign Role" id = "btnAssignRole"/>
                    }
                    else
                    {
                        <label>There are no roles to be assigned</label>
                     }                
                </td>
            </tr>


        }
    }

模型:

public class PageDetailsModel
    {

        public SelectList RoleList { get; set; }
        public int RoleId { get; set; }

        public SelectList AssignedRoleList { get; set; }
        public int DeassignRoleID { get; set; }

        public CommonLayer.Page CurrPage { get; set; }

        public string PageID { get; set; }

        public PageDetailsModel() { }

        public PageDetailsModel(string pageID)
            : this()
        {
            this.PageID = pageID;
            CurrPage = new BusinessLayer.Pages().getPageByID(pageID);

            BusinessLayer.Roles role = new BusinessLayer.Roles();
            AssignedRoleList = new SelectList(role.getAllPageRoleWithPageID(pageID), "Id", "Name");
            RoleList = new SelectList(role.getAllPageRolesByPageId(pageID), "Id", "Name");
        }

    }

您可以看幾件事。 這些屬性在查看html時具有什么樣的名稱? 它們是否遵循默認模型聯編程序的正確約定? 您還可以在調試時查看HttpContext.Current.Request下的表單值。 您是否看到所有值,例如RoleId? 如果名稱格式不正確,則可能必須在視圖中進行更改。 如果不可能,請考慮使用自定義模型綁定器。 這是ASP.Net MVC自定義模型綁定說明代碼項目鏈接的一些資源

對於調試,將控制器參數更改為FormCollection,如下所示:

 public ActionResult AssignRole(FormCollection formValues)

這樣,您可以看到表單中實際發回的內容。 MVC只是聰明的管道,它嘗試使用通過表單回傳的信息來填充模型對象。 如果它不在FormCollection中,則它將不會綁定到模型的屬性。

立即,我看到唯一回發到控制器的值是在下拉列表中選擇的角色的RoleID。 所有其他值都作為原始文本或HTML注入到頁面中,並且不綁定到回發。

如果在表單正文中的某處添加@ Html.HiddenFor(model => model.PageID,Model.PageID),則傳遞給控制器​​的模型將同時包含RoleID和PageID。 這應該足以執行功能(分配角色)並重建要傳遞回視圖的模型。

只有RoleID會發布在您的代碼中。 這些值與輸入和選擇字段一起發布。 您正在使用的唯一輸入字段是Role -selectlist。 純文本不會發布,就像您在td標簽中的文本一樣。

您需要為要發布的其他字段添加隱藏的輸入。 您可以為此使用HiddenFor和Hidden html幫助器。

@model OnlineLearningLTD.Models.PageDetailsModel

@{
        ViewBag.Title = "Page Details";
}

<h2>Page Details</h2>

<table class="table table-hover">
@using (Html.BeginForm("AssignRole", "Page", FormMethod.Post))
{
    if (Model.CurrPage != null)
    {

        <tr>
            <td>Name  @Html.HiddenFor(model => model.CurrPage.Name</td>
            <td>@Model.CurrPage.Name</td>
        </tr>
        <tr>
            <td>Url @Html.HiddenFor(model => model.CurrPage.Url</td>
            <td>@Model.CurrPage.Url</td>
        </tr> 
        <tr>
            <td>Assigned Roles:</td>
            @foreach(var role in @Model.CurrPage.Role)
            {
                <td>@role.Name</td>
            }
        </tr>     

        <tr>
            <td colspan="2"><h3>Assign Role</h3></td>
        </tr>
        <tr>
            <td>Choose Role</td>
            <td>
             @Html.DropDownListFor(model => model.RoleId, Model.RoleList)

            </td>
        </tr>       
        <tr>
            <td colspan="2" align="right">
                 @if(Model.RoleList.Count() > 0)
                {
                    <input type="submit" value="Assign Role" id = "btnAssignRole"/>
                }
                else
                {
                    <label>There are no roles to be assigned</label>
                 }                
            </td>
        </tr>
    }
}

現在,如果要發布CurrPage.Role集合,則將需要使用for-loop而不是foreach。 這將確保正確命名輸入,並且modelbinder可以解析模型。

    <tr>
            <td>Assigned Roles:</td>
            @for (int i = 0; i < Model.CurrPage.Role.Count(); i++)
            {
                <td>
                   @Html.HiddenFor(model => model.CurrPage.Role[i])
                   @role.Name
                </td>
            }
    </tr>  

暫無
暫無

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

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