Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 处理列表上的MVC4应用程序_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 4 处理列表上的MVC4应用程序

Asp.net mvc 4 处理列表上的MVC4应用程序,asp.net-mvc-4,Asp.net Mvc 4,这是我的模型类,有两个类Employee和Employee list namespace EditMultiplerecords.Models { public class Employee { public int Id { get; set; } public string Name { get; set; } } public class Employeelist : IEnumerable<Employee>

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

}
namespace EditMultiplerecords.Models
{
公营雇员
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类Employeelist:IEnumerable
{
公共列表雇员{get;set;}
公共IEnumerator GetEnumerator()
{
返回employee.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回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);*@
}
@model EditMultiplerecords.Models.Employeelist
@{
ViewBag.Title=“主页”;
}
$(函数(){
$('.editor输入').blur(函数(){
$(this.hide();
$(this).closest('p').find('label').html($(this.val()).show();
});
$('.editor label')。单击(函数(){
$(this.hide();
$(this).closest('p').find('input').show();
});
});
@foreach(模型中的var项目)
{
使用(Html.BeginForm())
{ 

@Html.HiddenFor(x=>item.Id)
@LabelFor(x=>item.Name,item.Name)
@EditorFor(x=>item.Name)

} @*@Html.Partial(“详细信息”,项目)*@ }
这个控制器类

  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);
    }
public ActionResult主页()
{
Employeelist el=新的Employeelist();
el.employee=新列表();
添加(新员工{Id=1,Name=“Rahul”});
添加(新员工{Id=2,Name=“America”});
添加(新员工{Id=3,Name=“NewJersey”});
返回视图(el);
}
[HttpPost]
公共行动结果主页(员工列表el)
{
返回视图(el);
}

我的问题是,当我编辑Rahul、America或NewJersey时,在操作后,我会得到一个空列表,其中包含空值,而不是一个更新的列表

您需要添加
@foreach(模型中的var项)
内部循环,使用(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);*@

请参阅更新的代码。我在错误的地方关闭表单。“提交”按钮需要在表单之外。这是我第一次能够显示数据,但在发布时,我无法存储更新的列表。它将变为空。对于每个项目,我都需要一个按钮,所以我将其保留在表单内部。我们将列表传递给模型,并将单个项从视图接收回控制器,以支持视图上的多个提交按钮。请参阅我的编辑我还想回邮上的剩余项目,我得到的是单个项目,因此总结一下,您将有每个记录的“创建表单”和“提交”按钮。在每次提交时,您希望以不同的形式将所有3条记录发送给Controller Action。我认为你需要从用户的角度重新审视你的需求。
[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();
}