How to pass List<Model> from View to Controller?

vortex3379 21 Reputation points
2022-10-02T21:31:58.097+00:00

I am having problems figuring out why my form posts nothing to the specified controller from the view? I am trying to send the controller multiple rows of form inputs.

public ActionResult CreateDetail(List<G2P_DataCollection_Detail> g2p_DCD_Model)  
        {  
            if (ModelState.IsValid)  
            {  
                foreach (G2P_DataCollection_Detail g2p_DCD in g2p_DCD_Model)  
                {  
                   //_context.G2P_DataCollections_Details.Add(myModel);  
                   _context.Add(g2p_DCD);  
                }  
                _context.SaveChanges();  
  
                return RedirectToAction("Index");  
            }  
  
            return View(g2p_DCD_Model);  
}  

        public IActionResult CreateDetail()  
        {  
            var dcId = Convert.ToInt32(TempData["Id"]); // # of form input rows to build  
            var myModels = new List<G2P_DataCollection_Detail>();              
            for (var i = -1; i < dcId; i++) // dynamically build input form using Model and #plants value.  
            {  
                myModels.Add(new G2P_DataCollection_Detail());  
            }  
            return View(myModels);  
}  

@using AuthSystem.Areas.Identity.Data  
@using AuthSystem.Models  
@model List<G2P_DataCollection_Detail>  
@{  
    ViewData["Title"] = "Create Detail";  
    Layout = "~/Views/Shared/_Layout_g2p_DC_Details.cshtml";  
}  
  
    @using (Html.BeginForm("CreateDetail", "G2P_DataCollection", FormMethod.Post))  
    {  
        @for (int i = 1; i < Model.Count; i++)  
        {  
            @Html.TextBoxFor(m => m[i].Id, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].G2P_DC_Id, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].GenotypeNumber, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].SeedlingReplicateNumber, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].GerminationDate, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].PhenotypingDate, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].Manual2LA, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].Manual3LA, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].Digital2LA, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].Digital3LA, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].Notes, new { @class = "form-control form-control-sm" })  
            @Html.TextBoxFor(m => m[i].TimeStamp, new { @class = "form-control form-control-sm" })            
              
        }  
        <input type="submit" value="Submit all" class="btn btn-primary" />  
    }  
</div>  

namespace AuthSystem.Models  
{  
    public class G2P_DataCollection_Detail  
    {         
        [Key]  
        public int Id { get; set; }  
        [Required]  
        public int G2P_DC_Id { get; set; }  
        [Required]  
        [Display(Name = "Genotype Number")]  
        public int GenotypeNumber { get; set; }  
        [Required]  
        [Display(Name = "Seedling Replicate Number")]  
        public int SeedlingReplicateNumber { get; set; }  
        [Required]  
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]  
        [DataType(DataType.Date)]  
        [Display(Name = "Germination Date")]  
        public DateTime GerminationDate { get; set; }  
        [Required]  
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]  
        [DataType(DataType.Date)]  
        [Display(Name = "Phenotyping Date")]  
        public DateTime PhenotypingDate { get; set; }  
        [Required]  
        [Display(Name = "Manual2LA")]  
        public decimal Manual2LA { get; set; }  
        [Required]  
        [Display(Name = "Manual3LA")]  
        public decimal Manual3LA { get; set; }  
        [Required]  
        [Display(Name = "Digital2LA")]  
        public decimal Digital2LA { get; set; }  
        [Required]  
        [Display(Name = "Digital3LA")]  
        public decimal Digital3LA { get; set; }          
        [Display(Name = "Notes")]  
        public string Notes { get; set; }  
        public DateTime TimeStamp { get; set; }  
    }  
}
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,207 questions
0 comments No comments
{count} votes

Accepted answer
  1. SurferOnWww 1,916 Reputation points
    2022-10-03T00:14:59.92+00:00

    To perform the model binding of collection properly , the name attribute of relevant html element must have index as follows:

    name="prefix[index].Property"

    prefix above must be g2p_DCD_Model in your case.

    Please verify your html generated by the ASP.NET.

    0 comments No comments

0 additional answers

Sort by: Most helpful