0

这是我的模型类,有两个类 Employee 和 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();
        }
    }

}

这是我正在编写代码以使用 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);*@
}

而这个控制器类

  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);
    }

我的问题是,当我在 Post Action 上编辑 Rahul 或 America 或 NewJersey 时,我得到一个空值列表而不是更新列表

4

1 回答 1

0

您需要在@foreach (var item in Model)里面添加循环using (Html.BeginForm())以接受修改后的列表

using (Html.BeginForm())
{

    @foreach (var item in Model)
    { 
    <div class="editor">
        <p>
            @Html.HiddenFor(x => item.Id)
            @Html.LabelFor(x => item.Name, item.Name)
            @Html.EditorFor(x => item.Name)
        </p>
    </div> 

    }
}
       <input type="submit" value="OK" />

@*        @Html.Partial("Details", item);*@

-- 编辑接受表单集合

[HttpPost]
public ActionResult Homepage(FormCollection formCollection)
{
    var itemid = formCollection.GetValue("item.id");
    var itemname= formCollection.GetValue("item.name");
---//use values to send it back to view----
    return View();
}
于 2012-07-12T07:21:58.130 回答