编辑多个子记录ASP.NET MVC的例子 [英] ASP.NET MVC example of editing multiple child records
问题描述
有谁知道一个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.
我尝试迄今已经得到我的地方,我可以显示的数据点,但我不能让它回发到控制器。我试图尽量缩小问题,我可以<一个href=\"http://stackoverflow.com/questions/709933/child-records-not-posting-on-asp-net-mvc-form\">here,但我仍然没有得到它,我觉得更广泛的例子可能是为了。任何想法?
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?
推荐答案
您可以尝试这样的事情。
You can try something like this.
假设你有这个对象:
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>
这将有助于正确地绑定在你的控制器中的表单数据模型的DefaultModelBinder。因此,的Response.Write(车辆[1]。颜色);
控制器上,将打印蓝
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".
这是一个很简单的例子,但我敢肯定你的想法。有关结合形式,数组,列表,集合,字典更多的例子,看看<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\">here.
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.
这篇关于编辑多个子记录ASP.NET MVC的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!