I have a partial view inside which loops my Availability object for getting values from text box. I have added my code in here.
My model class:
public class Availability
{
public int ID { get; set; }
public string Name{ get; set; }
public string Address { get; set; }
public string Gender { get; set; }
public int Adult { get; set; }
public int Child { get; set; }
}
My controller:
public class HomeController : Controller
{
List<Availability> lst = new List<Availability>();
public ActionResult Index()
{
Availability aa = new Availability();
Availability a1 = new Availability();
Availability a2 = new Availability();
Availability a3 = new Availability();
lst.Add(aa);
lst.Add(a1);
lst.Add(a2);
lst.Add(a3);
return View(lst);
}
public ActionResult ListOfPost(List<WebApplication1.Class.Availability> aaa)
{
return View();
}
}
My main view:
@model IEnumerable<WebApplication1.Class.Availability>
@{
ViewBag.Title = "Home Page";
}
My partail view:
<script type="text/javascript" src="http://code.jquery.com/jquery-3.3.1.js"></script>
Index page
@Html.Partial("_Availability", Model)
@model IEnumerable<WebApplication1.Class.Availability>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("ListOfPost", "Home", FormMethod.Post))
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Adult)
</th>
<th>
@Html.DisplayNameFor(model => model.Child)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.TextBoxFor(modelItem => item.Name, new { @class = "Name" })
</td>
<td>
@Html.TextBoxFor(modelItem => item.Address, new { @class = "Address" })
</td>
<td>
@Html.TextBoxFor(modelItem => item.Gender, new { @class = "Gender" })
</td>
<td>
@Html.TextBoxFor(modelItem => item.Adult, new { @class = "Adult" })
</td>
<td>
@Html.TextBoxFor(modelItem => item.Child, new { @class = "Child" })
</td>
</tr>
}
</table>
<input id="btnSubmit" class="btn btn-default" type="submit" value="Submit">
}
My error is - When I click on submit to post data to ListOfPost() ActionResult, parameter aaa is returning null value. I doubt that the cause of issue is name will be save for all text box.
How can I get user entered values in post???
What I have tried:
To differentiate name, I used below code.
@{int i = 0; }
@foreach (var item in Model)
{
<tr>
<td>
@Html.TextBoxFor(modelItem => item.Name, new { Name = item.Name + i, id = item.Name + i })
</td>
<td>
@Html.TextBoxFor(modelItem => item.Address, new { Name = item.Address + i, id = item.Address + i })
</td>
<td>
@Html.TextBoxFor(modelItem => item.Gender, new { Name = item.Gender + i, id = item.Gender + i })
</td>
<td>
@Html.TextBoxFor(modelItem => item.Adult, new { Name = item.Adult + i, id = item.Adult + i })
</td>
<td>
@Html.TextBoxFor(modelItem => item.Child, new { Name = item.Child + i, id = item.Child + i })
</td>
</tr>
@{ i += 1; }*@