MVC post a list of complex objects

前端 未结 5 1893
眼角桃花
眼角桃花 2020-11-30 06:41

I have a FeedbackViewModel that contains a list of questions:

public class FeedbackViewModel
{
    public List Questions { get; set;         


        
5条回答
  •  抹茶落季
    2020-11-30 07:28

    After much research I've found two solutions:

    1. One is to write HTML that has hardcoded Id's and Names
    2. Two is to convert your ICollection/IEnumerable to an Array or List (i.e IList something with an 'index'), and have an Array object in your BindingModel in your Controller POST Action.

    Thanks to Phil Haack's (@haacked) 2008 blog post http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ Which is still relevant to how the default ModelBinder works today for MVC. (NB: the links in Phil's article to sample porject and extension methods are broken)

    HTML snippet that inspired me:

    Post array looks a bit like:

    products.Index=cold&products[cold].Name=Beer&products[cold].Price=7.32&products.Index=123&products[123].Name=Chips&products[123].Price=2.23
    

    Model:

    public class CreditorViewModel
    {
        public CreditorViewModel()
        {
            this.Claims = new HashSet();
        }
        [Key]
        public int CreditorId { get; set; }
        public string Comments { get; set; }
        public ICollection 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 GET:

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

    Edit.cshtml:

    @Html.DisplayNameFor(m => m.Comments)
    @Html.EditorFor(m => m.Comments)
    
    
    @foreach (var item in Model.Claims)
    {
        var fieldPrefix = string.Format("{0}[{1}].", "Claims", item.CreditorClaimId);
        
        }
    
    @Html.DisplayNameFor(m => Model.Claims.FirstOrDefault().CreditorClaimType) @Html.DisplayNameFor(m => Model.Claims.FirstOrDefault().ClaimedTotalAmount)
    @Html.DisplayFor(m => item.CreditorClaimType) @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)
    @for (var itemCnt = 0; itemCnt < Model.ClaimsArray.Count(); itemCnt++) { @Html.TextBoxFor(m => Model.ClaimsArray[itemCnt].ClaimedTotalAmount) @Html.HiddenFor(m => Model.ClaimsArray[itemCnt].CreditorClaimId) }

    Form is processed in the Controller:

    Post Model:

    public class CreditorPostViewModel
    {
        public int CreditorId { get; set; }
        public string Comments { get; set; }
        public ICollection 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)
        {
            //...
    

提交回复
热议问题