如何使用EF DB Context在ViewModel中保存对象列表?

时间:2013-08-27 09:36:18

标签: c# entity-framework asp.net-mvc-4

我有一个模型women,它是我的数据库上下文的一部分,一个模型womenEditmodel包含一个women项列表。我使用partialview遍历此列表并在我的视图中显示可编辑的网格或列表。这些是我的模特:

public class Women
{
    public string ID { get; set; }
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

public class WomenEditModel
{
    public List<Women> WomenList { get; set; }
}

我的视图有这个循环,用于将women记录

注入我的视图行
@foreach (Women women in Model.Womens)
{
    Html.RenderPartial("WomenEditor", women);
}

我使用table显示。因此,现在用户可以编辑列表并发布或保存更改。

我的部分视图如下:

@model XXX.Models.Women

@using (Html.BeginCollectionItem("Women")) {                
    <td>
        @Html.HiddenFor(model => model.ID)
    </td>
    <td>
        @Html.TextBoxFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </td>
    <td>
        @Html.TextBoxFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </td>        
}

我的http post action方法如下所示

[HttpPost]
public ActionResult PostWomen(WomenEditModel model)
{
    /*I need to iterate through the returned list and save all
    changes to the db.*/

    return RedirectToAction("Index");
}

如何遍历在post action方法中收到的模型WomenEditModel并将对女性列表的更改保存回db?

提前致谢!!

2 个答案:

答案 0 :(得分:1)

我刚回到我的机器上。如果你还没有,你可以用它来实现它。

我的动作,呈现列表

    public ActionResult Index()
    {
        List<Women> womens = new List<Women>
            {
                new Women
                    {
                         Id=1,
                         FirstName = "Women1",
                         LastName = "Lastname1"
                    },
                new Women
                    {
                         Id=2,
                         FirstName = "Women2",
                         LastName = "Lastname2"
                    }
            };
        WomenList womenList=new WomenList();
        womenList.Womens = womens;
        return View(womenList);
    }

列表发布的操作。

    public ActionResult SaveWomens(List<Women> womenList)
    {
        System.Diagnostics.Debugger.Break();
        //Your save logic goes here
        return View("");
    }

部分视图(不知道是否需要)

@model List<MvcApplication1.Models.Women>
<table>
    @for (var i = 0; i < Model.Count; i++)
    {

        <tr>
            <td>
                @Html.TextBoxFor(m => m[i].Id)
            </td>
            <td>@Html.TextBoxFor(m => m[i].FirstName)
            </td>
            <td>@Html.TextBoxFor(m => m[i].LastName)
            </td>
        </tr> 
    }
</table>

这是视图

@model MvcApplication1.Models.WomenList
@{
    ViewBag.Title = "Home Page";
}
@section featured {
}
@using (Html.BeginForm("SaveWomens", "Home", FormMethod.Post))
{ 
    @Html.Partial("_Women", Model.Womens)

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

答案 1 :(得分:0)

您可以按照以下方式实现

 foreach (var item in womenList)
 {
     var obj = new Womens();
     //Assign values to obj for eg: obj.prop = item.prop
     dataContext.Womens.AddObject(obj);                  
 }
 dataContext.SaveChanges();