开发者

ASP.NET MVC Dropdown List won't populate Results

开发者 https://www.devze.com 2023-02-13 21:59 出处:网络
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 mo开发者_运

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 mo开发者_运维问答re 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消