none
How to bind DropdownList or Dropdownlistfor when the view containing them are in EditorTemplates folder? RRS feed

  • Question

  • I am developing an application in Asp.Net MVC using Entity Frameworks. Further, I have kept some of the views file inside EditorTemplates folder for they can be nested several times within the Create View for convenience.

    I have the following tables:

    1. Pensioner(Id (primary key), Name)

    2. Recipient (Id, PendionerId (foreign key))

    3. Spouse(Id (primary key), Name)

    4. SpouseRecipient (Id (primary key), ReceipientId (Foreign key), Date)

    I have the following Views:

    Index View:

    Here the data comes from the table pensioner where each row is provided with a button to assign the recipient for the pensioner.

    Create:

    Here depending on the PensionerId selected by clicking the button on the Index page, the Create View should display the name of the selected Pensioner. Since I want to create a new record in a Recipient Table for the selected Pensioner, I have nested the Recipient View inside EditorTemplates folder and using @Html.EditorForModel() method the Create View nests Recipient View. Similarly, View for SpouseRecipient has been housed inside the EditorTemplates so that it can be nested several times within the Create View depending on the number of spouse the pensioner has.

    The problem is when I bind dropdownlist as under in the Recipient View

    @Html.DropDownListFor(model => model.PensionerId, null, String.Empty,  new { @class = "form-control" })

    I get the following runtime error during POST:

    The ViewData item that has the key 'PensionerId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.

    When I use @Html.DropDownList("PensionerId", (SelectList) ViewBag.Pensioner, new { @class = "form-control" }) where in controller I use ViewBag for passing data to the view as under:

     var pensioner = from p in db.Pensioners
                                where p.PensionerId == id
                                select p;

    ViewBag.Pensioner = new SelectList(pensioner, "PensionerId", "Name");

    During the post again I get the following error:

    Cannot convert type 'string' to 'System.Web.Mvc.SelectList'

    Similarly, SpouseRecipient View which I will be nesting will also be carrying filtered list of spouses of the selected pensioner which I have to bind with dropdownlist or dropdownlistfor.

    In view of the above scenario, please help in finding the solution.

    Best Regards,

    Arun


     
    Friday, April 21, 2017 12:11 PM

Answers

  • Here is the solution for the problem cited above: 

    To pass on the selected pensioner to the dropdownlist:

    List<SelectListItem> PensionerList = new List<SelectListItem>();
                PensionerList.Add(new SelectListItem() { Value = pensioner.FirstOrDefault().PensionerId.ToString(), Text = pensioner.FirstOrDefault().Name });
                ViewBag.PensionerId = PensionerList;

    And in the View:

     @Html.DropDownListFor(m => m.PensionerId, ViewBag.PensionerId as IEnumerable<SelectListItem>, new { @class = "form-control" })

    The important thing to note is that ViewBag's property name should match the primary key name.

    • Marked as answer by ArunKhatri Sunday, April 23, 2017 7:10 AM
    Sunday, April 23, 2017 7:10 AM