MVC: Best way to populate Html .DropDownList

自作多情 提交于 2019-11-28 06:12:20

问题


I'm looking for a sound philosophy for bringing dynamic data into a view to populate a dropdownlist. Would it be a good idea to create a model object for dropdownlists and other "overhead" data or use a viewbag?

Thanks


回答1:


Using the ViewBag (as some have suggested in other answers/comments) to get data from your controller to view is generally seen as a code lack.

Your ViewModel should ideally contain all of the data you need for your view. So use your controller to populate this data on a property of your ViewModel:

Here is a simple example of how to create a drop down list in ASP.NET MVC using Html.DropDownListFor using model. You can do it like this all inlined in your *.cshtml file like so:

@Html.DropDownListFor(model => model.Package.State, new SelectList(
                  new List<Object>{ 
                       new { value = 0 , text = "Red"  },
                       new { value = 1 , text = "Blue" },
                       new { value = 2 , text = "Green"}
                    },
                  "value",
                  "text",
                   2))

which will create like this:

<select id="Package_State" name="Package.State"><option value="0">Red</option>
<option value="1">Blue</option>
<option value="2">Green</option>
</select>

Refer to this answer for more information




回答2:


Example for guidance:

I think the best way to achieve what you're after would be to use a ViewModel. You'd load the stuff you want to display in your View through this. So you'd create a dropdownlist with your accountlist which will be loaded in your controller. You'll also have your IEnumerable PErsoncontact in there which will also be loaded in your controller. Then your controller will pass the ViewModel to the View. You can use this as a guide.

ViewModel:

public class PersonViewModel
{
    public int PersonID {get;set;}
    public List<SelectListItem> PersonContactList {get;set;}
    public IEnumerable<TypesAvail> TypesAvails{get;set;}
}

Dropdownlist in Razor View :

@Html.DropDownListFor(m => m.PersonID , Model.PersonContactList )

Edit:- This is an example .Yes you can create a new class in same Model.

public class TypesAvail
{
     public String TypeNme { get; set; }
    public long TypeID { get; set; }
    public int NumSelected { get; set; }
    public int TypeCount { get; set; }
    public IEnumerable<SelectListItem> CarsAvail 
    {
        get
        {
            return new SelectList(
                    Enumerable.Range(0, TypeCount+1)
                    .OrderBy(typecount => typecount)
                    .Select(typecount => new SelectListItem
                    {
                        Value = typecount.ToString(),
                        Text = typecount.ToString()
                    }), "Value", "Text");
        }
    } 
}

Dropdownlist in Razor View :

@Html.DropDownListFor(m=> m.NumSelected, Model.CarsAvail)



回答3:


Including the data in the View's Model or in the ViewBag are both good options, the best one depends entirely on your specific use case.

If this dropdown should be included on only one (or just a few) pages, it makes sense to be part of the ViewModel.

If every page should have the dropdown (if it's part of the menu, or footer for instance) you could create a BaseController that supplies ViewBag data for the dropdown, and let your other controllers inherit from that:

BaseController.cs

public class BaseController : Controller
{
    public BaseController() {
        ViewBag.MyDroprown = ...
    }
}

Any other controller

// inheriting from BaseController will make ViewBag.MyDroprown accessible in the View
public class HomeController : BaseController
{
    // Any actions here
}



回答4:


In my opinion View model should contain all the data that is needed for rendering the view and that is available on view creation.

Using ViewBag for me is like using dynamic type in code - it gives you some flexibility but comes with the price of possible errors, so i try to avoid as much as possibe



来源:https://stackoverflow.com/questions/25861635/mvc-best-way-to-populate-html-dropdownlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!