ASP.NET MVC下拉列表不会填充结果

时间:2011-02-27 07:37:10

标签: c# asp.net-mvc drop-down-menu

好的,所以我花了很多时间试图找出如何为过滤器做下拉菜单。现在我有两个过滤器作为文本框,但我想要一个下拉列表,因为我将添加更多的过滤器。

这是我的索引页面:     

任务列表

<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> 

这是我在模型中所做的更改:

    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; }
    }

这是我所做的改变:

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

但是我无法填充任何下拉值。有人可以帮忙吗?

谢谢

2 个答案:

答案 0 :(得分:0)

试试这样:

[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);
}

然后在你的强类型视图中:

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

答案 1 :(得分:0)

我不知道您的类PapClientProfile和PapStaff,所以我用简单的字符串类型替换它们。 假设您的ViewModel定义如下:

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

然后,您将在视图中看到两个列表。一个包含ClientNames和 一个包含PhysicianNames。我认为这对你来说是对的吗?

您的控制器代码如下:

[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);
}

如果您的视图是强类型的,以将FilterViewMode作为模型,您可以使用以下代码显示下拉列表:


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

我个人喜欢在控制器中创建SelectLists,但它应该以任何一种方式工作。