select the value to populate html.dropdownlist

2019-08-23 03:55发布

问题:

I have two classes as follows

public class ODCTE_Major
{
    public int    ODCTE_MajorId { get; set; }
    public string OfficialMajorName      { get; set; }
    public string MajorCode { get; set; }

    ... More unrelated code ....
}

AND

public class CareerMajor
{
    ...lots of unrealted code to this question left out

    public int ODCTE_MajorId { get; set; }
    public virtual ODCTE_Major ODCTE_Major { get; set; }


}

I added a controller with CRUD methods and in the create.cshtml there is this line

<div class="editor-field">
    @Html.DropDownList("ODCTE_MajorId", String.Empty)
    @Html.ValidationMessageFor(model => model.ODCTE_MajorId)
</div>

The select list populates it with the OfficialMajorName from ODCTE_Major. I need the select list to populate with the MajorCode or a value that looks like MajorCode - OfficialMajorName.

Could someone provide assistance for how this is done, please?

Thanks.

回答1:

Add this to ODCTE_Major:

public string MajorDisplayName
{
    get { return string.Format("{0} - {1}", MajorCode, OfficialMajorName); }
}

This is just a read only property used to create the display text in the format you want the menu to use.

Then in CareerMajor, add:

public IEnumerable<ODCTE_Major> Majors{ set; get; } // Thank you Shyju!

This will give you a place in your view model to pass the list of Majors you want in your menu to the view.

Then in your action method when you're creating a CareerMajor view model to send to the view, populate the new IEnumberable with the ODCTE_Major entities you'd like displayed in your menu.

On the view page:

@Html.DropDownListFor(m => m.ODCTE_MajorId, new SelectList(Model.Majors, "ODCTE_MajorId", "MajorDisplayName", Model.ODCTE_MajorId), "Select One")

This creates a SelectList to populate the drop down with. The SelectList constructor is saying use ODCTE_MajorId as the value for a SelectListItem in the menu, and to use MajorDisplayName as the text to actually display in the menu. It sets the selected value, if there is one, and adds a null item with the text "Select One" to the top of the menu. Feel free to take that final argument out if you don't want the null text.



回答2:

Have your ViewModel hold a Collection property to represent all available Majors (for poulating the Dropdown)

public class CareerMajor
{  
  //other proiperties
   public int ODCTE_MajorId { get; set; }
   public IEnumerable<ODCTE_Major> Majors{ set; get; }
} 

And in your GET Action, fill it and send it to your strongly typed view

pubilc ACtionResult Create()
{
  var viewModel=new CareerMajor();
  viewModel.Majors=db.GetllAllMajors();  // getting a list of ODCTE_Major objects
  return View(viewModel);
}

and in the View, use the DropDownListFor HTML Helper method.

@model CareerMajor
@Html.BeginForm())
{
   @Html.DropDownListFor(m=>m.ODCTE_MajorId, 
        new SelectList(Model.Majors,"ODCTE_MajorId ","MajorCode"),"select one..")
  //other elements
}


回答3:

In your controller action:

ViewBag.ODCTE_MajorId = new SelectList(availableMajors, "ODCTE_MajorId", "MajorCode");

*second and third parameters are the names of the value and text fields respectively

Then in your view:

 @Html.DropDownList("ODCTE_MajorId", String.Empty)

where availableMajors is an IEnumerable that contains the majors you want to list.