I want to pass all values from dynamically generated text-boxes from view to controller.
My model:
public class QuestionModel
{
[Required(ErrorMessage = "{0} is required")]
[Display(Name = "Question here")]
public string Question { get; set; }
}
My view:
@using (Html.BeginForm("Add_Question", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<div class="form-group">
//here I'm generating dynamic textboxes
@for (int i = 1; i <= numberOfQuestions; i++)
{
<div class="col-md-12">
@Html.LabelFor(model => model.Question, new { })
@Html.TextBoxFor(model => model.Question, "", new { @required = "required", @class = "form-control" })
@Html.ValidationMessageFor(model => model.Question, "", new { @class = "text-danger" })
</div>
}
</div>
<div class="form-group">
<div class="col-md-12">
<input type="submit" value="Done" class="btn-success form-control" />
</div>
</div>
}
My controller:
public ActionResult Add_Question()
{
return View();
}
[HttpPost]
public ActionResult Add_Question(QuestionModel model)
{
//Get all textbox values here
return RedirectToAction("Home", "Home");
}
Should I create a list of strings for this? If yes then how? Please help.