• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 488
  • Last Modified:

passing data in mvc

What would an mvc model look like that is passing a list like for a dropdownlist and also passing other data like firstname lastname? Do you have to use viewmodel
2 Solutions
green_thumb_98Author Commented:
Is the answer just to use viewdata for passing the dropdownlist items?  Then use the model for everything else like firstname, lastname, etc.?
Craig WagnerSoftware ArchitectCommented:
If you're looking for a "right" answer to this question you're going to be disappointed because there isn't one.

You could use ViewData to pass the drop down items and a model to pass the editable fields, or you could use a viewmodel to pass everything. It really just depends on how you want to handle it and what the standards of your organization are for this type of thing.

If it were entirely up to me I'd create a viewmodel to pass everything. That way I get a strongly-typed view and get Intellisense for all the items in the viewmodel when I'm writing the view.
You can use the view model to pass through the list, so for example your model would have

List<TypeOfObject MyListOfItems { get; set;}

And you could then call that whenever needed from your controller, and assign it in the view.

EG controller
var myModel = new MyViewModel();
myModel.MyListOfItems = [Whatever way you're getting your list].

And in your view (going off memory) you could just assign it out as @html.DropDownList(model => model.MyListOfItems, "Default item")
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now