i am new to mvc. i want to send list of data from view to controller. i can pass one employee details but can't pass list . please give some suggestion for sort it out.
i need to get the Lidt in controller.
public class EmpDetails
{
public int Id { get; set; }
public string Name { get; set; }
public int OldSeq { get; set; }
public int NewSeq { get; set; }
}
public class DetailsList
{
public int Id { get; set; }
public IEnumerable<EmpDetails> EmpDetailsList { get; set; }
}
In controller have two method get and post. In post method getting id but not list.it is coming as null.
[HttpGet]
public ActionResult EmployeeDetails()
{
List<EmpDetails> c2 = new List<EmpDetails>();
EmpDetails model = new EmpDetails();
model = new EmpDetails();
model.Id = 2;
model.Name = "Alex";
model.OldSeq = 4;
model.NewSeq = 5;
c2.Add(model);
DetailsList abc = new DetailsList();
abc.Id = 1;
abc.EmpDetailsList = c2;
return View(abc);
}
[HttpPost]
public ActionResult EmployeeDetails(DetailsList model)
{
return View("EmployeeDetails");
}
In view i'm looping the data and displaying.
@using MvcDemo.Models
@model DetailsList
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.EditorFor(modelItem => modelItem.Id)
<table>
@foreach (var item in Model.EmpDetailsList)
{
<tr>
<td>
@Html.EditorFor(modelItem => item.Id)
</td>
<td>
@Html.TextBoxFor(modelItem => item.Name)
</td>
<td>
@Html.TextBoxFor(modelItem => item.OldSeq)
</td>
<td>
@Html.TextBoxFor(modelItem => item.NewSeq)
</td>
</tr>
}
</table>
<input type="submit" value="Create" />
}