选择一个值到 DropDownList 中

发布于 2024-12-10 02:14:06 字数 321 浏览 3 评论 0原文

我有以下声明:

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

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

感谢您的帮助!

I have the following statement:

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

Suppous that my controller sent to the aspx page that contais this dropdownlist a variable with the value "3", how can I set this dropdownlist to select this option when the page loads ?

Thanks for the help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

输什么也不输骨气 2024-12-17 02:14:06

使用视图模型:

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

Use view models:

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

then have your controller populate this view model:

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

and finally in your strongly typed view:

<%= Html.DropDownListFor(x => x.SelectedCityId, Model.Cities) %>
梦纸 2024-12-17 02:14:06

如果您的控制器传递“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>) %>

这样,选择正确城市背后的所有逻辑都在控制器中,就像从一开始就应该的那样。

If your controller is passing '3' why not making it pass the all list of SelectListItem instead?

Then you would do something like this in or controller:

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

And received it this way within the view:

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

This way the all logic behind selecting the proper city is in the controller, as it should since the beginning.

假情假意假温柔 2024-12-17 02:14:06

创建 selectlistitem 时,只需执行以下操作:

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

When you create the selectlistitem, just do:

new SelectlistItem { Text = "City1", Value = 1, Selected=true}
黑色毁心梦 2024-12-17 02:14:06
<%: Html.DropDownList("dropCity", new[] { 
      new SelectlistItem { Text = "City1", Value = 1, Selected = true}, 
      new SelectlistItem { Text = "City2", Value = 2}, 
      new SelectlistItem { Text = "City3", Value = 3}})%> 
<%: Html.DropDownList("dropCity", new[] { 
      new SelectlistItem { Text = "City1", Value = 1, Selected = true}, 
      new SelectlistItem { Text = "City2", Value = 2}, 
      new SelectlistItem { Text = "City3", Value = 3}})%> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文