【问题标题】:Posting back a list, The list is always null回发一个列表,该列表始终为空
【发布时间】:2014-06-10 14:53:22
【问题描述】:

我正在尝试在 MVC 中回发一个可编辑的列表,但是对于我的原始视图,该列表始终返回 null。我有一个视图模型列表,所有结果都显示在一个表单中,其中有一些是可编辑的。我做了一些谷歌搜索,发现这个http://forums.asp.net/t/1848323.aspx?MVC+Model+List+Httppost+always+null 几乎是同一个问题。我尝试用IList 植入它,但我仍在发回一个空列表。任何想法为什么或如何发回列表?有任何问题或是否需要更多信息/代码,请告诉我。谢谢。

使用 IEnumerable 查看原件

@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "frm", id = "frm" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)    

    foreach (var item in Model)
    {
    <!--  Partial View Form -->
    @Html.Partial("~/Views/Home/Partial/PartialEdit.cshtml", item)  
    <hr />
}


<div>
    <p>
        <input type="submit" id="submitButton" value="Save" />
    </p>
</div>

}

查看当前以使用 IList

@model IList<NavLiveWebInterface.ViewModel.Proc_Item_SKUViewModel>
for (var i = 0; i < Model.Count(); i++)
{
    <!--  Partial View Form -->
    @Html.Partial("~/Views/Home/Partial/PartialEdit.cshtml", Model[i])  
    <hr />
}

局部视图

@model NavLiveWebInterface.ViewModel.Proc_Item_SKUViewModel

<div class="row">
    <div class="col">
        <div class="editor-label">
            @Html.LabelFor(model => model.Item_SKU)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Item_SKU,new { disabled="disabled", @readonly = "readonly" })
        </div>
    </div>
    <div class="col">
        <div class="editor-label">
            @Html.LabelFor(model => model.Master_SKU)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Master_SKU,new { disabled="disabled", @readonly = "readonly" })
        </div>
    </div>
    <div class="col">
        <div class="editor-label">
            @Html.LabelFor(model => model.Attribute_Code)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Attribute_Code,new { disabled="disabled", @readonly = "readonly" })
        </div>
    </div>
    <div class="col">
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Name,new { disabled="disabled", @readonly = "readonly" })
        </div>
    </div>
    <div class="col">
        <div class="editor-label">
            @Html.LabelFor(model => model.Sample_Quantity)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Sample_Quantity)
        </div>
    </div>
    <div class="col">
        <div class="editor-label paddinglabel">
            @Html.LabelFor(model => model.Usage)
        </div>
        <div class="paddingdropdown">
            <div class="editor-field dropdown">
                @Html.DropDownListFor(model => model.Usage, Model.TrueFalse)
                @Html.ValidationMessageFor(model => model.Usage)
            </div>
        </div>
    </div>
    <div class="col">
        <div class="editor-label paddinglabel">
            @Html.LabelFor(model => model.Filterable)
        </div>
        <div class="paddingdropdown">
            <div class="editor-field dropdown">
                @Html.DropDownListFor(model => model.Filterable, Model.TrueFalse)
                @Html.ValidationMessageFor(model => model.Filterable)
            </div>
        </div>
    </div>
    <div class="col">
        <div class="editor-label paddinglabel">
            @Html.LabelFor(model => model.Display)
        </div>
        <div class="paddingdropdown">
            <div class="editor-field dropdown">
                @Html.DropDownListFor(model => model.Display, Model.TrueFalse)
                @Html.ValidationMessageFor(model => model.Display)
            </div>
        </div>
    </div>
    <div class="col">
        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Description,new { disabled="disabled", @readonly = "readonly" })
        </div>
    </div>
</div>

控制器 HTTP POST

[HttpPost]
public ActionResult Edit(IList<Proc_Item_SKUViewModel> vmList)
{
    try
    {
        if (ModelState.IsValid)
        {
            //updates the items
            _repository.updateItems(vmList);                

            return RedirectToAction("../Home/Index");
        }
    }
    catch (Exception e)
    {
        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
    }
    return View(vmList);
}

【问题讨论】:

    标签: c# asp.net razor


    【解决方案1】:

    我仍然认为问题在于您的输入没有被索引。就像答案@http://forums.asp.net/t/1848323.aspx?MVC+Model+List+Httppost+always+null

    您正在使用 Model[i] 渲染部分,但在部分内部,输入字段将与模型中其他项目的输入字段冲突。只需查看生成的 HTML,您可以看到输入字段将具有重复的名称和/或 ID(并且它们也没有索引器)

    我认为这不会像当前的设置方式那样与 PartialView 一起使用。最简单的方法是丢失 PartialView 并将 HTML 放入循环中并使用以下结构:

    @for (var i = 0; i < Model.Count; i++)
    {
        @Html.TextBoxFor(m => Model[i].Master_SKU)
    }
    

    注意indexer @Html.TextBoxFor(m => Model [i] ).Master_SKU

    【讨论】:

    • 感谢您的信息和建议,效果很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2021-06-15
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多