Is there any way to pass list of models, each generated by HTML
Partial View
to a Controller Action
in ASP.Net MVC 5
?
So I have a View
(with a model called WordModel
) where I call a HTML partial View
(with a model called MeaningModel
) multiple times in a form like the following:
@using (Html.BeginForm("Create", "Entry", FormMethod.Post, new { id = "createForm" })) {
@Html.AntiForgeryToken()
<!--some other things here-->
@Html.Partial("_Meaning", new MeaningModel() { Number = 1 })
@Html.Partial("_Meaning", new MeaningModel() { Number = 2 })
@Html.Partial("_Meaning", new MeaningModel() { Number = 3 })
<!--some other things here-->
}
And the MeaningModel
consists of multiple basic elements like the following:
public class MeaningModel {
public string MeaningValue { get; set; }
public int HomonimNumber { get; set; }
public string Example1 { get; set; }
public string Example2 { get; set; }
//and so on
}
Now, in my Controller
, I have an Action
which handles the Create
form submission:
// POST: Meaning/Create
[HttpPost]
public ActionResult Create(WordModel model, List<MeaningModel> meanings, FormCollection collection) {
try {
// TODO: Add insert logic here
return RedirectToAction("Index");
} catch {
return View();
}
}
I could get the values I put in the main View
in the WordModel model
, but not the values in the Partial Views
which consists of elements forming the List<MeaningModel> meanings
. Is there any way to access them?
I could give each element in the MeaningModel
different name per meaning model (such as MeaningValue_1
, MeaningValue_2
, MeaningValue_3
, HomonimNumber_1
, HomonimNumber_2
, HomonimNumber_3
, and so on) so that they will be captured by the FormCollection
. But, as much as possible, I want to take advantage of the MeaningModel
I have created and get them by the model.
Any way to do that?