MVC 4 Create View - Ordering Drop Down for Forgein Key table

I have an MVC4 project using EF6 to which I have added a Controller, Model and related Views using the VS 2012 wizard.

The Create View includes this code (.aspx engine)

    <div class="editor-label">
        <%: Html.LabelFor(model => model.senProviderId, "Site Id") %>
    <div class="editor-field">
        <%: Html.DropDownList("senProviderId", String.Empty) %>
        <%: Html.ValidationMessageFor(model => model.senProviderId) %>

where model.senProviderId references a related (foreign key) table and produces this HTML (segment)

        <div class="editor-field">
        <select id="senProviderId" name="senProviderId"><option value=""></option>
        <option value="3">SW127     </option>
        <option value="4">NE011     </option>
        <option value="5">NE133     </option>
        <option value="6">Out016    </option>
        <option value="104">SE133     </option>

For obvious reasons I need to order the Drop-Down by the Display Value rather then by the underlying key.

How can that best be done?

EDIT: As requested Controller code for the parent table Controller

  public ActionResult Create()
        ViewBag.senHousingCode = new SelectList(db.sssHousingCodes, "hcId", "hcName");
        ViewBag.senProviderId = new SelectList(db.sssProviderProfiles, "prvId", "prvSiteId");
        return View();


OF course - I was looking for decoration on the Model but of course the answer is using OrderBy in the controller code:

 ViewBag.senProviderId = new SelectList(db.sssProviderProfiles.OrderBy(m => m.prvSiteId), "prvId", "prvSiteId");

thanks for the prompt Mikhail!

Need Your Help

Help printing a barcode. Looking for best practice


I want to print a barcode that is generated by our system. I have a couple of concerns though and would like some input on the right way to do this if my solution is not right.

can a mobile application developed for io6 work correctly on ios7 and ios8?

ios ios7 ios6 ios8

Could you please indicate us if an Mobile Application developed for iOS6 can work correctly (without any adaptation) on iOS 7 and 8 or there will always be any functional and display issues and the...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.