【问题标题】:ASP.NET MVC Set the posted value to dropdownlistASP.NET MVC 将发布的值设置为下拉列表
【发布时间】:2020-04-08 21:47:21
【问题描述】:

我正在为我的应用程序使用 select2 ddlist。我想在提交页面后显示选定的值。sumbit 返回也是同一页面的 View()。

@Html.DropDownList("DatabaseNames")
@Html.DropDownList("TableNames")
<button type="submit" class="btn btn-primary">Check/button>


 $(document).ready(function () {
        $("#DatabaseNames").select2();
        $("#TableNames").select2();
    });

//控制器

 [HttpPost]
    public ActionResult Profiles(FormCollection form)
    {   
       return View();
    }

我想在提交页面后将所选值设置为此下拉列表。 谢谢

【问题讨论】:

    标签: asp.net caching model-view-controller jquery-select2


    【解决方案1】:

    您可以从 FormCollection 中获取价值并在 ViewBag 中返回

    准备SelectListItems,您可以在其中设置Selected属性如下并通过ViewBag发送查看

     List<SelectListItem> dropdownItems = new List<SelectListItem>();
        dropdownItems.Add(new SelectListItem() { Value = "1", Text = "Item 1", Selected = false });
        dropdownItems.Add(new SelectListItem() { Value = "2", Text = "Item 2", Selected = true });
    

    然后在视图中检索 dropdownItems 并呈现如下

    @Html.DropDownList("elementName", dropdownItems);
    

    【讨论】:

    • 是的,我可以获取此值并将其添加到 ViewBag。但我怎样才能把这个值作为一个选择
    猜你喜欢
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多