Save dynamically created controls in asp.net mvc database

I create controls using a viewbag that has a list of items

public ActionResult Create() { var savedAdditionalFields = afs.getAllStudentFieldss(1); ViewBag.AdditionalFieldList = savedAdditionalFields; return View(); } 

In the view, I use

Create

 @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>StudentAdditionalDetail</legend> @foreach (MyClass item in ViewBag.AdditionalFieldList) { <div> @Html.DisplayFor(itemlist=>item.Name) @Html.EditorFor(model=>model.AdditionalInfo) </div> } 

Controls and labels are created. Now I save the items in the database using

 [HttpPost] public ActionResult Create(StudentAdditionalDetail additionalFields) { ads.AddAdditionalDetails(additionalFields);//calls the insert method return RedirectToAction("Index"); } 

But only the first data in the text field is stored in the database. I think I need to repeat my method of creating [HttpPost]. But not sure where I should put the code that will iterate through my entire Create view and all the elements in

model.AdditionalInfo

will be saved in the database

+4
source share
1 answer

Try the following:

 [HttpPost] public ActionResult Create(List<StudentAdditionalDetail> additionalFields) { ads.AddAdditionalDetails(additionalFields);//calls the insert method return RedirectToAction("Index"); } 
0
source

Source: https://habr.com/ru/post/1500245/


All Articles