ASP.NET MVC 编辑多个子记录的示例

发布于 2024-07-16 02:46:34 字数 366 浏览 6 评论 0原文

有谁知道 MVC 视图的任何示例或教程,该视图在一个表单上显示所有父/子数据,并允许所有子记录可编辑?

例如,假设我有一个人员表和另一个包含他们拥有的车辆的表。 一对一的形式,我想显示给定人的每辆车,并使数据元素可编辑(即车牌号、汽车颜色等),以防出现错误。 我不想跳到每辆车的单独编辑表单。

到目前为止,我的尝试已经使我能够显示数据,但无法将其发送回控制器。 我已尽力缩小问题范围 这里,但我仍然没有明白,我认为可能需要一个更广泛的例子。 有任何想法吗?

Does anyone know of any examples or tutorials of an MVC view that shows parent/child data all on one form, and allows all the child records to be editable?

For example, say I have a table of people and another containing the vehicles they own. One one form, I want to show every vehicle for a given person, and make the data elements editable (i.e. license plate number, car color, etc.) in case there are mistakes. I don't want to jump to a separate edit form for each vehicle.

My attempts thus far have gotten me to the point where I can display the data, but I can't get it to post back to the controller. I've tried to narrow down the problem as far as I could here, but I'm still not getting it, and I think a broader example may be in order. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-07-23 02:46:34

你可以尝试这样的事情。

假设您有这个对象:

public class Vehicle
{
    public int VehicleID { get; set; }
    public string LicencePlate { get; set; }
    public string Color { get; set; }
}

这是您将用来编辑车辆详细信息的控制器操作(您将在其中发布表单):

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditVehicles(int Owner, Vehicle[] vehicles)
{
    //manipulate the data, then return back to the list
    return RedirectToAction("YourAction");
}

那么您应该这样设置表单:

<!--have a form for each person, listing their vehicles-->
<form action="/EditVehicles" method="post">
    <input type="hidden" name="Owner" value="25" />
    <input type="hidden" name="Vehicles[0].VehicleID" value="10" />
    <input type="text" name="Vehicles[0].LicencePlate" value="111-111" />
    <input type="text" name="Vehicles[0].Color" value="Red" />
    <input type="hidden" name="Vehicles[1].VehicleID" value="20" />
    <input type="text" name="Vehicles[1].LicencePlate" value="222-222" />
    <input type="text" name="Vehicles[1].Color" value="Blue" />
    <input type="submit" value="Edit" />
</form>

这将帮助 DefaultModelBinder 正确绑定表单数据到控制器中的模型。 因此,控制器上的 Response.Write(vehicles[1].Color); 将打印“Blue”。

这是一个非常简单的例子,但我相信您已经明白了。 有关将表单绑定到数组、列表、集合、字典的更多示例,请查看此处

You can try something like this.

Suppose you have this object :

public class Vehicle
{
    public int VehicleID { get; set; }
    public string LicencePlate { get; set; }
    public string Color { get; set; }
}

And this is your controller action that you'll use to edit vehicle details (where you'll post the form) :

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditVehicles(int Owner, Vehicle[] vehicles)
{
    //manipulate the data, then return back to the list
    return RedirectToAction("YourAction");
}

Then you should set your form this way :

<!--have a form for each person, listing their vehicles-->
<form action="/EditVehicles" method="post">
    <input type="hidden" name="Owner" value="25" />
    <input type="hidden" name="Vehicles[0].VehicleID" value="10" />
    <input type="text" name="Vehicles[0].LicencePlate" value="111-111" />
    <input type="text" name="Vehicles[0].Color" value="Red" />
    <input type="hidden" name="Vehicles[1].VehicleID" value="20" />
    <input type="text" name="Vehicles[1].LicencePlate" value="222-222" />
    <input type="text" name="Vehicles[1].Color" value="Blue" />
    <input type="submit" value="Edit" />
</form>

This will help the DefaultModelBinder to correctly bind the form data to your model in your controller. Thus Response.Write(vehicles[1].Color); on your controller, will print "Blue".

This is a very simple example, but I'm sure you get the idea. For more examples about binding forms to arrays, lists, collections, dictionaries, take a look at here.

画离情绘悲伤 2024-07-23 02:46:34

我认为解决这个问题的最佳方法是使用 AJAX 帖子,每当用户单击提交按钮时,您都可以挂钩该事件,创建一个包含您想要保留的内容的 JSON 数组并将其发送出去。

当然,另一种方法是在 POST 操作中从“FormCollection”对象获取所有信息。 您只需要迭代所有键解析数据,然后处理它。

I think the best you can get around this is by using AJAX posts, Whenever the user clicks on the submit button, You can hook in that event, create a JSON array of the things you want to persist and send it across.

The other way of course is to get all the information from the "FormCollection" object in the POST action. You just need to iterate through all the keys parse the data and then process it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文