更新同一视图中的多个列表项

时间:2016-10-20 07:15:52

标签: asp.net-mvc-4

我正在尝试将默认值设为应用程序,我的视图使用一个编辑器加载我的所有值。我的控制器没有从视图中获取任何数据?

我希望能够同时编辑所有价值吗?我怎么能这样做

型号代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Demo.Models.Admin
{
    public class MerchantPackageTransactionModel
    {
        public MerchantPackageTransactionModel()
        {
            FeatureList = new List<PackageFeatureModel>();
        }
        public int PackageId { get; set; }
        public string PackageName { get; set; }
        public string Image { get; set; }

        public List<PackageFeatureModel> FeatureList { get; set; }
    }

    public class PackageFeatureModel
    {
        public int FeatureId { get; set; }
        public string FeatureName { get; set; }
        public string Type { get; set; }
        public string DefaultValue { get; set; }
        public string Value { get; set; }
    }
}    

查看代码

@model Demo.Models.Admin.MerchantPackageTransactionModel

@{
    ViewBag.Title = "CreatePackageTransaction";
    Layout = "~/Themes/green/Views/Shared/_AdminDashboard.cshtml";
}

@using (Html.BeginForm())
{

    <fieldset>
        <legend>MerchantPackageTransactionModel</legend>

        @Html.HiddenFor(model => model.PackageId)
        <div class="editor-label">
            @Html.LabelFor(model => model.PackageName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PackageName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Image)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Image)
        </div>

        @foreach (var item in Model.FeatureList)
        {

            @Html.HiddenFor(model => item.FeatureId)

            <div class="editor-label">
                @Html.Label("Feature Name")
            </div>
            <div class="editor-field">
                @Html.DisplayFor(model => item.FeatureName)
            </div>

            <div class="editor-label">
                @Html.Label("Type")
            </div>
            <div class="editor-field">
                @Html.DisplayFor(model => item.Type)
            </div>

            <div class="editor-label">
                @Html.Label("Default Value")
            </div>
            <div class="editor-field">
                @Html.DisplayFor(model => item.DefaultValue)
            </div>

            <div class="editor-label">
                @Html.Label("Value")
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => item.Value)
            </div>
        }

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

控制器代码

public ActionResult CreatePackageTransaction(MerchantPackageTransactionModel objMerchantPackageTransactionModel)
{
    foreach (var item in objMerchantPackageTransactionModel.FeatureList)
    {
        if (ModelState.IsValid)
        {
            GR.InsertOrUpdate(item);
        }
    }
    GR.Save();
    return View(objMerchantPackageTransactionModel);
}

1 个答案:

答案 0 :(得分:0)

发生了什么事,因为你正在使用

@Html.EditorFor(model => item.Value)

这是一个与模型直接关联的命令,它用于渲染未从模型(项目)访问的输入,它正在呈现带有“错误”名称的输入html:

<input class="text-box single-line" id="item_Value" name="item.Value" type="text" value="">

什么时候应该

<input class="text-box single-line" id="FeatureList_0__Value" name="FeatureList[0].Value" type="text" value="">

并且,由于名称与模型(FeatureList)中的名称不匹配,当请求到达服务器时,框架无法执行自动绑定。

您有两个选项可以解决它:

  • 手动创建html,设置正确的(预期)名称;或者:

  • 在迭代中使用索引,以便EditorFor生成具有预期名称的输入:

    @{
        var index = 0;
    }
    
    @foreach (var item in Model.FeatureList)
    {
    
        @Html.HiddenFor(model => model.FeatureList[index].FeatureId)
    
        <div class="editor-label">
            @Html.Label("Feature Name")
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.FeatureList[index].FeatureName)
        </div>
    
        <div class="editor-label">
            @Html.Label("Type")
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.FeatureList[index].Type)
        </div>
    
        <div class="editor-label">
            @Html.Label("Default Value")
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.FeatureList[index].DefaultValue)
        </div>
    
        <div class="editor-label">
            @Html.Label("Value")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FeatureList[index].Value)
        </div>
    
        index++;
    }
    

我已经测试过,它的工作正常。周末愉快!问候。