This is my model class with two classes Employee and Employee list
namespace EditMultiplerecords.Models
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Employeelist : IEnumerable<Employee>
{
public List<Employee> employee { get; set; }
public IEnumerator<Employee> GetEnumerator()
{
return employee.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return employee.GetEnumerator();
}
}
}
This is my View where i am writing code for editing using javascript
@model EditMultiplerecords.Models.Employeelist
@{
ViewBag.Title = "Homepage";
}
<link href="../../Content/StyleSheet1.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('.editor input').blur(function () {
$(this).hide();
$(this).closest('p').find('label').html($(this).val()).show();
});
$('.editor label').click(function () {
$(this).hide();
$(this).closest('p').find('input').show();
});
});
</script>
@foreach (var item in Model)
{
using (Html.BeginForm())
{
<div class="editor">
<p>
@Html.HiddenFor(x => item.Id)
@Html.LabelFor(x => item.Name, item.Name)
@Html.EditorFor(x => item.Name)
<input type="submit" value="OK" />
</p>
</div>
}
@* @Html.Partial("Details", item);*@
}
And this controller class
public ActionResult Homepage()
{
Employeelist el = new Employeelist();
el.employee = new List<Employee>();
el.employee.Add(new Employee { Id = 1, Name = "Rahul" });
el.employee.Add(new Employee { Id = 2, Name = "America" });
el.employee.Add(new Employee { Id = 3, Name = "NewJersey" });
return View(el);
}
[HttpPost]
public ActionResult Homepage(Employeelist el)
{
return View(el);
}
My problem is when i edit Rahul or America or NewJersey, on Post Action iam getting an empty list with null values rather an updated list
you need to add
@foreach (var item in Model)loop insideusing (Html.BeginForm())to accept modified List