如何更新的IEnumerable<&MyObject的GT;在MVC? [英] How to updating IEnumerable<MyObject> in mvc?

查看:72
本文介绍了如何更新的IEnumerable<&MyObject的GT;在MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有型号为MyObject,然后在视图中我有

I have model MyObject and then in the view I have

@model IEnumerable<MyProject.Models.MyObject>

和再往形式:

@using(Html.BeginForm())
{
    @foreach(MyObject m in Model)
    {
        @Html.TextboxFor(x => x.Name)
    }
}

<input type="submit" value="modify" />

我想在我的控制器这样获得的:

I am trying to get this in my controller like this:

[HttpPost]
public void Update(MyObject _myOject)
{
}

当我看到源$ C ​​$ C,所有名称重复和_myObject不正确填充

When I see the source code, all name is repeated and _myObject is not correctly populated

如何更新对象的列表,并在一次性全部储存在MVC?

how do I update a list of objects and save them all at once in MVC?

推荐答案

您的操作方法是等待名为_myObject一个参数。而你想更新的对象列表

You action method is waiting for a single parameter named _myObject. And you want to update list of objects

尝试是这样的:

@using(Html.BeginForm(......))
{
    int i = 0;
    foreach(MyObject m in Model)
    {
        @Html.TextBox(string.Format("_myObjects[{0}].Name", i), m.Name)
        i++;
    }

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

和您的操作方法应该是这样的:

and your action method should look like that:

[HttpPost]
public void Update(IList<MyObject> _myObjects)
{
}

,那么你就可以立刻更新此列表

then you will be able to update this list at once

这篇关于如何更新的IEnumerable&LT;&MyObject的GT;在MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆