C# MVC Razor 5:模型作为null从视图传递给控制器

C# MVC Razor 5:模型作为null从视图传递给控制器,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一个视图,它的模型是ViewModel,用于显示一些数据。对于以这种方式显示的每一行,用户可以从下拉列表中选择一个值,然后单击按钮将其选择保存到数据库中每次点击按钮都可以通过这种方式处理多个“选择”。 为此,我使用了一个Html.BeginForm方法,如so(名为altered): 我在控制器中的方法是: [HttpPost] [ValidateAntiForgeryToken] public ActionResult DatabaseSave(AttributionView

我有一个视图,它的模型是ViewModel,用于显示一些数据。对于以这种方式显示的每一行,用户可以从下拉列表中选择一个值,然后单击按钮将其选择保存到数据库中每次点击按钮都可以通过这种方式处理多个“选择”。

为此,我使用了一个Html.BeginForm方法,如so(名为altered):

我在控制器中的方法是:

[HttpPost]
[ValidateAntiForgeryToken]       
public ActionResult DatabaseSave(AttributionViewModel attr)
{
   // Data processing.
}
但当我使用它时,attr为null。我仍然可以通过使用Request.Form和正确的索引来获取数据,但这不是我更愿意使用的解决方案。我有一个模型可以这样做,并且专门使用了ViewModel,所以我不必询问Request.Form。但我就是看不出我错过了什么。有人能帮我吗

为了快速找到答案,您将在下面找到所有相关类和视图的伪代码

类,该类包含以下数据:

public class Attribution
{
    public Attribution()
    {
        listToAttribute = new List<SelectListItem>();
    }
    public string Title { get; set; }
    public int ID { get; set; }
    public string Description { get; set; }
    public int? Number { get; set; }
    public IEnumerable<SelectListItem> listToAttribute { get; set; }
    public int AttributedNumber { get; set; } // Mostly used internally to properly send dropdownlist data.
    // Other methods that are not relevant.
    }
公共类属性
{
公众归因()
{
listToAttribute=新列表();
}
公共字符串标题{get;set;}
公共int ID{get;set;}
公共字符串说明{get;set;}
公共整数?数字{get;set;}
public IEnumerable listToAttribute{get;set;}
public int AttributedNumber{get;set;}//主要在内部用于正确发送dropdownlist数据。
//其他不相关的方法。
}
视图中使用的ViewModel:

public class AttributionViewModel : Attribution
{
    public AttributionViewModel()
    {

    }
    public IPagedList<Attribution> attributedList { get; set; }
}
公共类属性ViewModel:属性
{
公共属性ViewModel()
{
}
公共IPagedList attributedList{get;set;}
}
该视图的相关部分:

 @model Project.ViewModels.AttributionViewModel 
 @using PagedList.Mvc
 @using PagedList

// Some code later...

            @using (Html.BeginForm("DatabaseSave ", "Save", new { attr = Model.attributedList }, FormMethod.Post))
        {
                        @Html.ValidationSummary(false, "", new { @class = "text-danger" })
            @Html.AntiForgeryToken()
            <table class="contentTable contentTable-evo fixedTableHeader">
                <thead>
                   // Headers are not relevant.
                </thead>
                <tbody id="processingTable">
                    @{
                        int i = 0;
                    }
                    @foreach (var item in Model.attributedList)
                    {

                        <tr>
                            <text>@Html.EditorFor(modelItem => Model.attributedList[i].ID, new { htmlAttributes = new { @class = "form-control", @hidden = "hidden", @style = "width:0px display:none", @type = "hidden" } })</text>
                            <td class="noWrap width1percent tri">@item.Title</td>
                            <td class="noWrap width1percent tri">@item.ID</td>
                            <td class="noWrap width1percent tri">@item.Description</td>
                            <td class="noWrap width1percent tri">@item.Number</td>
                            <td class="noWrap width1percent tri">
                                <text>@Html.DropDownListFor(modelItem => Model.attributedList[i].AttributedNumber, Model.attributedList[i].listToAttribute , "" , new { htmlAttributes = new { @class = "form-control", @style = "width:90px" } })</text>
                            </td>
                        </tr>
                        i++;
                    }
                </tbody>
            </table>
            <br />
            <center>
                <input value="Processing" class="add" type="submit" onclick="javascript:return ShowConfirm();" >
            </center>

            Page @(Model.attributedList.PageCount < Model.attributedList.PageNumber ? 0 : Model.attributedList.PageNumber) out of @Model.attributedList.PageCount | @Model.attributedList.TotalItemCount <br />
        <text>@Resources.Views.PaginationResource.GoToPage </text>@Html.DropDownList("PageChanger", new SelectList(Enumerable.Range(1, Model.attributedArticles.PageCount), "", ""), new { onchange = "PageChangedFromDropDown()" })

        @Html.PagedListPager(Model.attributedList, page => Url.Action("Attribute", new { page, searchString = Session["Search"], currentFilter = ViewBag.CurrentFilter, sortColumn = ViewBag.sortAttr }), new PagedListRenderOptions { LiElementClasses = new[] { "needsLoading" }, DisplayEllipsesWhenNotShowingAllPageNumbers = false })
@model Project.ViewModels.attributeionviewmodel
@使用PagedList.Mvc
@使用页面列表
//稍后会有一些代码。。。
@使用(Html.BeginForm(“DatabaseSave”,“Save”,new{attr=Model.attributedList},FormMethod.Post))
{
@Html.ValidationSummary(false,“,new{@class=“text danger”})
@Html.AntiForgeryToken()
//标题不相关。
@{
int i=0;
}
@foreach(Model.attributedList中的var项)
{
@Html.EditorFor(modeleItem=>Model.attributedList[i].ID,new{htmlAttributes=new{@class=“form control”、@hidden=“hidden”、@style=“width:0px display:none”、@type=“hidden”})
@项目.标题
@item.ID
@项目.说明
@项目编号
@Html.DropDownListFor(modeleItem=>Model.attributedList[i].AttributedNumber,Model.attributedList[i].listToAttribute,“,new{htmlAttributes=new{@class=“form control”,@style=“width:90px”})
i++;
}

第@页@(Model.attributedList.PageCount @Resources.Views.PaginationResource.GoToPage@Html.DropDownList(“页面转换器”,新选择列表(Enumerable.Range(1,Model.AttributeArticles.PageCount),“”,“”,“”),新{onchange=“PageChangedFromDropDown()”) @Html.PagedListPager(Model.attributedList,page=>Url.Action(“属性”,新建{page,searchString=Session[“Search”],currentFilter=ViewBag.currentFilter,sortColumn=ViewBag.sortAttr}),新建PagedListRenderations{LiElementClasses=new[]{“needsLoading”},显示EllipsWhennotShowingAllPageNumber=false})
多亏了斯蒂芬·穆克,我才发现自己做错了什么

问题出现在ViewModel中:

    public IPagedList<Attribution> attributedList { get; set; }
公共IPagedList attributedList{get;set;} 我需要使用它,因为ViewModel通常不能使用页面列表

但是,这并不意味着它可以用来包含和解析数据。我需要一个单独的列表,添加以下内容:

    public IPagedList<Attribution> attributedList { get; set; }
    public List<Attribution> parsableListOfAttributed { get; set; } // Otherwise passing to the controller WILL NOT WORK.
公共IPagedList attributedList{get;set;} public List parsableListOfAttributed{get;set;}//否则传递给控制器将不起作用。 对于任何与PagedList有关的操作(例如,在我上面的示例中,我需要它来计算页面数),我可以引用attributedList。但是当将它传递给控制器时,这将不起作用,控制器仍然认为它是PagedList,因此不能用于此目的

更正视图:

        @using (Html.BeginForm("DatabaseSave", "Save", FormMethod.Post))
        {
                        @Html.ValidationSummary(false, "", new { @class = "text-danger" })
            @Html.AntiForgeryToken()
            <table class="contentTable contentTable-evo fixedTableHeader">
                <thead>
                    // Headers are still not relevant.
                </thead>
                <tbody id="processingTable">
                    @{
                        int i = 0;
                    }
                    @foreach (var item in Model.attributedArticles)
                    {

                        <tr>
                            <text>@Html.EditorFor(modelItem => Model.parsableListOfAttributed[i].ID, new { htmlAttributes = new { @class = "form-control", @hidden = "hidden", @style = "width:0px display:none", @type = "hidden" } })</text>
                            <td class="noWrap width1percent tri">@item.GTIN</td>
                            <td class="noWrap width1percent tri">@item.SMP</td>
                            <td class="noWrap width1percent tri">@item.DescriptionFr</td>
                            <td class="noWrap width1percent tri">@item.AuthorisationNumber</td>
                            <td class="noWrap width1percent tri">
                                <text>@Html.DropDownListFor(modelItem => Model.parsableListOfAttributed[i].AttributedNumber, Model.parsableListOfAttributed[i].listToAttribute, "" , new { htmlAttributes = new { @class = "form-control", @style = "width:90px"} })</text>
                            </td>
                        </tr>
                        i++;
                    }
                </tbody>
            </table>
            <br />
            <center>
                <input value="Processing"" class="add" type="submit" onclick="javascript:return ShowConfirm();" >
            </center>

                        }
@使用(Html.BeginForm(“DatabaseSave”、“Save”、FormMethod.Post))
{
@Html.ValidationSummary(false,“,new{@class=“text danger”})
@Html.AntiForgeryToken()
//标题仍然不相关。
@{
int i=0;
}
@foreach(Model.attributedArticles中的var项)
{
@EditorFor(modelItem=>Model.ParsableListofAttribute[i].ID,新的{htmlAttributes=new{@class=“form control”、@hidden=“hidden”、@style=“width:0px display:none”、@type=“hidden”})
@item.GTIN
@项目1.SMP
@item.DescriptionFr
@项目.授权编号
@Html.DropDownListFor(modeleItem=>Model.parsableListOfAttributed[i]。AttributedNumber,Model.pars
        @using (Html.BeginForm("DatabaseSave", "Save", FormMethod.Post))
        {
                        @Html.ValidationSummary(false, "", new { @class = "text-danger" })
            @Html.AntiForgeryToken()
            <table class="contentTable contentTable-evo fixedTableHeader">
                <thead>
                    // Headers are still not relevant.
                </thead>
                <tbody id="processingTable">
                    @{
                        int i = 0;
                    }
                    @foreach (var item in Model.attributedArticles)
                    {

                        <tr>
                            <text>@Html.EditorFor(modelItem => Model.parsableListOfAttributed[i].ID, new { htmlAttributes = new { @class = "form-control", @hidden = "hidden", @style = "width:0px display:none", @type = "hidden" } })</text>
                            <td class="noWrap width1percent tri">@item.GTIN</td>
                            <td class="noWrap width1percent tri">@item.SMP</td>
                            <td class="noWrap width1percent tri">@item.DescriptionFr</td>
                            <td class="noWrap width1percent tri">@item.AuthorisationNumber</td>
                            <td class="noWrap width1percent tri">
                                <text>@Html.DropDownListFor(modelItem => Model.parsableListOfAttributed[i].AttributedNumber, Model.parsableListOfAttributed[i].listToAttribute, "" , new { htmlAttributes = new { @class = "form-control", @style = "width:90px"} })</text>
                            </td>
                        </tr>
                        i++;
                    }
                </tbody>
            </table>
            <br />
            <center>
                <input value="Processing"" class="add" type="submit" onclick="javascript:return ShowConfirm();" >
            </center>

                        }