【问题标题】:Select a value into a DropDownList在 DropDownList 中选择一个值
【发布时间】:2011-10-15 16:31:07
【问题描述】:

我有如下声明:

<%: Html.DropDownList("dropCity", new[] { new SelectlistItem { Text = "City1", Value = 1}, new SelectlistItem { Text = "City2", Value = 2}, new SelectlistItem { Text = "City3", Value = 3}})%> 

假设我的控制器向包含此下拉列表的 aspx 页面发送了一个值为“3”的变量,我该如何设置此下拉列表以在页面加载时选择此选项?

感谢您的帮助!

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-2


    【解决方案1】:

    使用视图模型:

    public class MyViewModel
    {
        public string SelectedCityId { get; set; }
        public IEnumerable<SelectListItem> Cities { get; set; }
    }
    

    然后让您的控制器填充此视图模型:

    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // preselect the second city
            SelectedCityId = "2",
            Cities = new[]
            {
                new SelectListItem { Value = "1", Text = "City1" },
                new SelectListItem { Value = "2", Text = "City2" },
                new SelectListItem { Value = "3", Text = "City3" },
            }
        };
        return View(model);
    }
    

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

    <%= Html.DropDownListFor(x => x.SelectedCityId, Model.Cities) %>
    

    【讨论】:

    • @Dan-SP,在 ASP.NET MVC 中推荐的方法是始终使用视图模型。总是。
    【解决方案2】:

    如果您的控制器传递“3”,为什么不让它传递 SelectListItem 的所有列表?

    然后你会在或控制器中做这样的事情:

    public ActionResult MyController(MyModel model) 
    {
        // Build SelectListItem list
        ViewBag.CitiesList = new List<SelectListItem>() 
        {
            new SelectlistItem { Text = "City1", Value = 1, Selected = true}, 
            new SelectlistItem { Text = "City2", Value = 2}, 
            new SelectlistItem { Text = "City3", Value = 3}
        }
    
        View(model);
    }
    

    并在视图中以这种方式接收它:

    <%: Html.DropDownList("dropCity", ViewBag.CitiesList as IEnumerable<SelectListItem>) %>
    

    这样,选择合适城市的所有逻辑都在控制器中,从一开始就应该这样做。

    【讨论】:

      【解决方案3】:

      当您创建选择列表项时,只需执行以下操作:

      new SelectlistItem { Text = "City1", Value = 1, Selected=true}
      

      【讨论】:

        【解决方案4】:
        <%: Html.DropDownList("dropCity", new[] { 
              new SelectlistItem { Text = "City1", Value = 1, Selected = true}, 
              new SelectlistItem { Text = "City2", Value = 2}, 
              new SelectlistItem { Text = "City3", Value = 3}})%> 
        

        【讨论】:

        • 感谢您的及时回复!问题是我不知道来自控制器的选项.. 一次可能是 1.. 在另一次刷新时可能是 2.. 或 3..
        • @Dan-SP - 我不明白你的问题。您知道来自控制器的值,因为它来自控制器。执行 Selected = myvalue == 1、selected = myvalue == 2 等操作。
        • 它来自控制器,这就是为什么我不能设置 "Text = "City1", Value = 1, Selected = true" 因为在任何其他情况下我都可以获得来自控制器的值 2。 .
        • @Dan-SP - 如果您希望我们提供与您提供的代码示例不同的解决方案,这不是很有帮助。您要求解决硬编码价值问题。如果您的真实代码不是硬编码的,请提供一个示例来显示您的真实代码,否则我们只是在浪费时间。
        • 那是真正的代码。我得到了一些其他的样本,但我使用了一个模型,并且这个模型很好地工作。问题是我不会使用模型来解决我的问题,检查: model.code, new SelectList(Model.dropDwonList, "Code", "Name", VALUE_FROM_CONTROLLER ), "选择一个选项", new { style = "width:180px"})%>
        猜你喜欢
        • 1970-01-01
        • 2020-03-03
        • 1970-01-01
        • 1970-01-01
        • 2017-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多