开发者

can't get data from model binding with a listbox that represents a child table

I've been my head against this for most of the day. I don't think I understand enough of the moving pieces in MVC and I need some help getting to a solution regarding listboxes and data binding.

Essentially, I want to use a modal dialog to select for a grid o开发者_开发百科f data, and then have the selection show up in a listbox. (that works). I then want to POST that list box to the controller and have child records for each item that was in the list box (doesn't work).

How can I get all the 's from a elegantly in MVC and get them into the database?

[Setup]

I have an object, WorkOrder, that represents a table in SQL. WorkOrder has a collection, WorkerRequests, that represents a child table. WorkOrder is the parent.

I have a form that creates new WorkOrders and allows the addition of WorkerRequests through a modal dialog. WorkerRequest records are shown in a listbox after selected with the dialog.

The dialog uses javascript's HTMLOptionsCollection add(Element) function to add an element. That works.

When I post, the data binder is tossing the added element. I can't seem to find the raw data in the HttpContext, and the binder isn't making it available.


Your listbox items if not selected will not be posted over. You ideally would need them selected OR create them in a hidden form that gets posted to your controller method.

Include a parameter in your action method named

[HttpPost]
public ActionResult Index(FormsCollection collection )
{
    var item = collection["whateverName"];
}

See if it shows up there as a starting point. Its hard to see exactly whats going on without your code here.

Your model dialog does what - adds to the listbox? Does your dialog post back to your controller to create the dialog? Are your dialog contents inside a form? If you use jQuery's dialog and your dialog is inside a form be aware jQuery will actually pop it outside of your form when it shows your dialog , you have to add it back to your form again. If this is a case for you, I'll update with those details but lets make sure that is/is not whats happening here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜