asp.net-mvc - MVC 发布复杂对象列表

标签 asp.net-mvc forms inheritance post complex-data-types

我有一个包含问题列表的 FeedbackViewModel:

public class FeedbackViewModel
{
    public List<QuestionViewModel> Questions { get; set; }
}

这个 QuestionViewModel 是一个对象,可以被 5 种不同类型的问题继承
public class QuestionViewModel
{
    public string QuestionText { get; set; }
    public string QuestionType { get; set; }
}

继承问题类型之一的示例:
public class SingleQuestionViewModel : QuestionViewModel
{
    public string AnswerText { get; set; }
}

HttpGetIndex Controller 中的操作我从数据库中获取问题并在 FeedbackViewModel 的问题列表中添加正确的问题类型然后我在 View 中渲染这个模型:
@using (Html.BeginForm())
{
    //foreach (var item in Model.Questions)
    for (int i = 0; i < Model.Questions.Count; i++)
    {
        <div class="form-group">
            @Html.DisplayFor(modelItem => Model.Questions[i].QuestionText, new { @class = "control-label col-md-4" })
            <div class="col-md-6">
                @if (Model.Questions[i].QuestionType == "Single")
                {
                    @Html.EditorFor(modelItem => (Model.Questions[i] as OpenDataPortal.ViewModels.SingleQuestionViewModel).AnswerText)
                }
                else if (Model.Questions[i].QuestionType == "Multiple")
                {
                    @Html.TextAreaFor(modelItem => (Model.Questions[i] as OpenDataPortal.ViewModels.SingleQuestionViewModel).AnswerText)
                }
                else if (Model.Questions[i].QuestionType == "SingleSelection")
                {
                    @Html.RadioButtonForSelectList(modelItem => (Model.Questions[i] as OpenDataPortal.ViewModels.SingleSelectionQuestionViewModel).SelectedAnswer,
                                                                (Model.Questions[i] as OpenDataPortal.ViewModels.SingleSelectionQuestionViewModel).SelectionAnswers)
                }
                else if (Model.Questions[i].QuestionType == "MultipleSelection")
                {
                    @Html.CustomCheckBoxList((Model.Questions[i] as OpenDataPortal.ViewModels.MultipleSelectionQuestionViewModel).AvailableAnswers)
                }
                else if (Model.Questions[i].QuestionType == "UrlReferrer")
                {
                    @Html.EditorFor(modelItem => (Model.Questions[i] as OpenDataPortal.ViewModels.SingleQuestionViewModel).AnswerText)
                }
            </div>
        </div>
        <br />
    }

    <br />
    <button type="submit">Submit</button>
}

enter image description here

现在,我根本无法让它在模型中发布问题列表。甚至可以发布不同对象类型的列表吗?

编辑:以下是我使用 Fiddler 发现的帖子中的数据列表:

enter image description here

最佳答案

经过大量研究,我找到了两种解决方案:

  • 一种是编写具有硬编码 ID 和名称的 HTML
  • 二是将您的 ICollection/IEnumerable 转换为数组或列表(即 IList 带有“索引”的东西),并在 Controller POST 操作中的 BindingModel 中有一个数组对象。

  • 感谢 Phil Haack (@haacked) 2008 年的博文 http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
    这仍然与今天默认的 ModelBinder 如何为 MVC 工作有关。
    (注意:Phil 文章中示例项目和扩展方法的链接已损坏)

    启发我的 HTML 片段:
    <form method="post" action="/Home/Create">
        <input type="hidden" name="products.Index" value="cold" />
        <input type="text" name="products[cold].Name" value="Beer" />
        <input type="text" name="products[cold].Price" value="7.32" />
    
        <input type="hidden" name="products.Index" value="123" />
        <input type="text" name="products[123].Name" value="Chips" />
        <input type="text" name="products[123].Price" value="2.23" />
    
        <input type="submit" />
    </form>
    

    Post 数组看起来有点像:
    products.Index=cold&products[cold].Name=Beer&products[cold].Price=7.32&products.Index=123&products[123].Name=Chips&products[123].Price=2.23
    

    模型:
    public class CreditorViewModel
    {
        public CreditorViewModel()
        {
            this.Claims = new HashSet<CreditorClaimViewModel>();
        }
        [Key]
        public int CreditorId { get; set; }
        public string Comments { get; set; }
        public ICollection<CreditorClaimViewModel> Claims { get; set; }
        public CreditorClaimViewModel[] ClaimsArray { 
            get { return Claims.ToArray(); }
        }
    }
    
    public class CreditorClaimViewModel
    {
        [Key]
        public int CreditorClaimId { get; set; }
        public string CreditorClaimType { get; set; }
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N2}")]
        public Decimal ClaimedTotalAmount { get; set; }
    }
    

    Controller 获取:
    public async Task<ActionResult> Edit(int id)
        {
            var testmodel = new CreditorViewModel
            {
                CreditorId = 1,
                Comments = "test",
                Claims = new HashSet<CreditorClaimViewModel>{
                    new CreditorClaimViewModel{ CreditorClaimId=1, CreditorClaimType="1", ClaimedTotalAmount=0.00M},
                    new CreditorClaimViewModel{ CreditorClaimId=2, CreditorClaimType="2", ClaimedTotalAmount=0.00M},
                }
            };
            return View(model);
        }
    

    编辑.cshtml:
    @Html.DisplayNameFor(m => m.Comments)
    @Html.EditorFor(m => m.Comments)
    
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(m => Model.Claims.FirstOrDefault().CreditorClaimType)
            </th>
            <th>
                @Html.DisplayNameFor(m => Model.Claims.FirstOrDefault().ClaimedTotalAmount)
            </th>
        </tr>        
    <!--Option One-->
    @foreach (var item in Model.Claims)
    {
        var fieldPrefix = string.Format("{0}[{1}].", "Claims", item.CreditorClaimId);
        <tr>
            <td>
                @Html.DisplayFor(m => item.CreditorClaimType)
            </td>
            <td>
            @Html.TextBox(fieldPrefix + "ClaimedTotalAmount", item.ClaimedTotalAmount.ToString("F"),
            new
            {
                @class = "text-box single-line",
                data_val = "true",
                data_val_number = "The field ClaimedTotalAmount must be a number.",
                data_val_required = "The ClaimedTotalAmount field is required."
            })
            @Html.Hidden(name: "Claims.index", value: item.CreditorClaimId, htmlAttributes: null)
            @Html.Hidden(name: fieldPrefix + "CreditorClaimId", value: item.CreditorClaimId, htmlAttributes: null)
            </td>
        </tr>
        }
    </table>    
    <!--Option Two-->
    @for (var itemCnt = 0; itemCnt < Model.ClaimsArray.Count(); itemCnt++)
    {
        <tr>
            <td></td>
            <td>
                @Html.TextBoxFor(m => Model.ClaimsArray[itemCnt].ClaimedTotalAmount)
                @Html.HiddenFor(m => Model.ClaimsArray[itemCnt].CreditorClaimId)
        </td></tr>
    }
    

    表单在 Controller 中处理:

    岗位型号:
    public class CreditorPostViewModel
    {
        public int CreditorId { get; set; }
        public string Comments { get; set; }
        public ICollection<CreditorClaimPostViewModel> Claims { get; set; }
        public CreditorClaimPostViewModel[] ClaimsArray  { get; set; }
    }
    
    public class CreditorClaimPostViewModel
    {
        public int CreditorClaimId { get; set; }
        public Decimal ClaimedTotalAmount { get; set; }
    }
    

    Controller :
    [HttpPost]
        public ActionResult Edit(int id, CreditorPostViewModel creditorVm)
        {
            //...
    

    关于asp.net-mvc - MVC 发布复杂对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26155607/

    相关文章:

    asp.net-mvc - ASP.net MVC Controller - 构造函数的使用

    asp.net-mvc - 为什么 System.ComponentModel.DataAnnotations.DisplayAttribute 被密封?

    javascript - 来自数据库 MVC 5 的自动完成文本框

    php - 如何在 Laravel 5.2 中检索下拉菜单表中的数据字段

    c# - 确保此代码文件中定义的类与 'inherits' 属性匹配

    c# - 任何实现类似于 "abstract static member"的东西的方法

    c# - 如何在客户端不发送任何请求的情况下从 Windows Azure 服务器向客户端移动应用程序(iOS)发送消息?

    jquery - 在通过ajax提交之前使用jquery验证来验证表单

    php - 使用 zend 框架将表单从弹出窗口提交到原始窗口

    c++ - 在 C++ 中创建从 io 类继承的异常类