gpt4 book ai didi

c# - MVC5 如何从用户写入下拉值

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:43 28 4
gpt4 key购买 nike

好的,我的下拉框开始工作了,我有我的选项,如果我选择任何一个,我想写下提交的所选项目的值,听起来很简单吧?好吧,每次我单击任何选项并提交时,该值都会显示在 URL 上,但我似乎无法将其写出来...

View (索引.cshtml):

<p>Dropdown Using viewbag with Html.DropDownList </p>
<br/>
<div>
@using (Html.BeginForm("dropDown", "Home", FormMethod.Get))
{
@Html.DropDownList("ListItem")

<button type="submit"></button>
}

</div>

<p>@ViewBag.SelectedValue</p> //should print the value I selected but it does nothing...

Controller (HomeController.cs):

public ActionResult Index()
{
List<SelectListItem> ObjItem = new List<SelectListItem>()
{
new SelectListItem {Text="Select",Value="0",Selected=true },
new SelectListItem {Text="ASP.NET",Value="1" },
new SelectListItem {Text="C#",Value="2"},
new SelectListItem {Text="MVC",Value="3"},
new SelectListItem {Text="SQL",Value="4" },
};
ViewBag.ListItem = ObjItem;

return View("Index");
}

public ActionResult dropDown(List<SelectListItem> ListItem)
{
ViewBag.SelectedValue = ListItem[0].Value; //crossing fingers this has the value I want to print


return this.View("Index");
}

最佳答案

这样试试

View (索引.cshtml):

<div>
@using (Html.BeginForm("dropDown", "Home", FormMethod.Post))
{
@Html.DropDownList("ListItem")

<button type="submit">Submit</button>
}

</div>

<p>@ViewBag.SelectedValue</p>

Controller (HomeController.cs):

        public ActionResult Index()
{
bindCombo();
return View();
}

private void bindCombo()
{
List<SelectListItem> ObjItem = new List<SelectListItem>()
{
new SelectListItem {Text="Select",Value="Select",Selected=true },
new SelectListItem {Text="ASP.NET",Value="ASP.NET" },
new SelectListItem {Text="C#",Value="C#"},
new SelectListItem {Text="MVC",Value="MVC"},
new SelectListItem {Text="SQL",Value="SQL" },
};
ViewBag.ListItem = ObjItem;
}

[HttpPost]
public ActionResult dropDown(string ListItem)
{
ViewBag.SelectedValue = ListItem;
bindCombo();
return View("index");
}

关于c# - MVC5 如何从用户写入下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37314048/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com