简体   繁体   中英

ASP.NET MVC Dropdown List won't populate Results

Ok so I've spent a lot of time trying to figure out how to do a drop down for filters. Right now I have two filters as a textbox but I want a dropdown list instead as I would be adding few more filters.

This is my index page:

Task List

<ul>
<% using (Html.BeginForm("Index", "Task"))
   {%>
    <fieldset> 
        <legend>Filters</legend> 
        Date Range: From
        <%: Html.TextBox("StartDT") %> 
        To
        <%: Html.TextBox("EndDT") %> <br />
        <%: Html.DropDownList("ClientName", "PhysicianName") %> 

    <input type="submit" name="Filter" value="Filter" /> 
    <input type="submit" name="ClearFilter" value="Clear Filter" /> 
    </fieldset> 
<% } %>
</ul> 

This is the change I made in my Model:

    public class FilterViewModel
    {
        public PapClientProfile Client { get; set; }
        public PapStaff Physician { get; set; }
        public IEnumerable<SelectListItem> ClientName { get; set; }
        public IEnumerable<SelectListItem> PhysicianName { get; set; }
    }

This is the change I made in:

    [AcceptVerbs(HttpVerbs.Get)]
    public ViewResult Edit()
    {
        var model = new FilterViewModel { PossibleValues = PapClientProfile("ClientName"), PapStaff ("PhysicianName") };
    }

But I'm not able to populate any dropdown values. Can anybody please help?

Thank you

Try like this:

[HttpGet]
public ActionResult Edit()
{
    var model = new FilterViewModel 
    { 
        PossibleValues = new[]
        {
            new SelectListItem { Value = "1", Text = "value 1" },
            new SelectListItem { Value = "2", Text = "value 2" },
            new SelectListItem { Value = "3", Text = "value 3" },
        }
    };
    return View(model);
}

and then in your strongly typed view:

<%: Html.DropDownListFor(
    x => x.Physician.PhysicianName, 
    new SelectList(Model.PossibleValues, "Value", "Text")
) %> 

I do not know your classes PapClientProfile and PapStaff, so I substituted them with simple string types. Assuming your ViewModel is defined like so:

 
    public class FilterViewModel
    {
        public string ClientName { get; set; }
        public string PhysicianName { get; set; }
        public IEnumerable ClientNames { get; set; }
        public IEnumerable PhysicianNames { get; set; }
    }

You will then have TWO lists available in the view. One containing ClientNames and one containing PhysicianNames. I'll assume this is correct for you?

Your controller code is like this:

[HttpGet]
public ActionResult Edit()
{
    var model = new FilterViewModel 
    { 
        Client = "ClientID-2",
        Physician = "",

        ClientName = new[]
        {
            new SelectListItem { Value = "ClientID-1", Text = "Client Name 1" },
            new SelectListItem { Value = "ClientID-2", Text = "Client Name 2" },
            new SelectListItem { Value = "ClientID-3", Text = "Client Name 3" },
        },

        PhysicianName = new[]
        {
            new SelectListItem { Value = "PhysicianID-1", Text = "Physician Name 1" },
            new SelectListItem { Value = "PhysicianID-2", Text = "Physician Name 2" },
            new SelectListItem { Value = "PhysicianID-3", Text = "Physician Name 3" },
        }

   };
    return View(model);
}

If your view is strongly typed to get your FilterViewMode as model you can use the following code to display a dropdown list:


<%: Html.DropDownListFor(
    x => x.ClientName, 
    new SelectList(Model.ClientNames, "Value", "Text")
) %>
<br/>
<%: Html.DropDownListFor(
    x => x.PhysicianName, 
    new SelectList(Model.PhysicianNames, "Value", "Text")
) %> 

Personally I like to create the SelectLists in the controller, but it should work either way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM