views:

21

answers:

2

Hi all,

I have a simple (hopefully) scenario.

  • Seats table
  • Computers table
  • SeatComputers table (since a seat can have multiple computers assigned)

I have a strongly typed "Edit" view for "Seats". I have managed to get a multi select list on that page in order to assign or unassign computers (jquery to add/remove items).

However, when I submit the form, the contents of select list are not posted to the controller action.

I assume this is because "computers" select list is not a model property.

Is there anyway to POST the additional data to the controller outside of model's properties?

My tables look something like this: alt text

A: 

I usually create ViewModels that combine whatever properties from the Entity models that the view needs, then point at that for Visual Studio to generate the initial view.

so instead of System.Web.Mvc.ViewPage<Seats> it would be System.Web.Mvc.ViewPage<SeatEditorViewModel>

That way, whatever properties your view needs don't even have to be a part of an entity.

BioBuckyBall
Thanks for the suggestion Lucas. For this one module, I think I'm gonna stick to ViewModel that's generated by LINQ. However, I will definitely be using this technique for some of the more complex pages.
nsr81
+1  A: 

You don't need to post this list as you already have it stored into the database and you even have a repository to fetch it, haven't you? So the only thing that needs to be posted is the user selection as this is the only thing you don't know. In the POST action reconstruct the list in your view model using a repository, the same way you did in the GET action that rendered the form.

Is there anyway to POST the additional data to the controller outside of model's properties?

Sure, simply include them as input fields so that their values are sent along the POST and in your controller action:

[HttpPost]
public ActionResult Index(SomeViewModel model, string param1, string, param2)
{
    ...
}

But I insist once again: you don't need this in your case.

Darin Dimitrov
Wow... I feel dumb now... All I had to do was to add a JQuery snippet to "selelct" all the items in my multi-select list before submission. It's now working. You words about "posting user selection" clicked. Thanks.
nsr81