如何从下拉列表中移动到另一个视图.净mvc
本文关键字:另一个 视图 mvc 移动 下拉列表 | 更新日期: 2023-09-27 17:50:40
我被困在这里一个多星期了。我试图创建一个下拉列表,当用户从下拉列表单击某些值,它会重定向到另一个视图称为索引。下面是我的下拉代码。下拉列表中的值从SQL Server中获取。我该怎么做,在控制器还是视图中改变?
@Html.DropDownListFor(model => model.Reference, ViewBag.ISharedUI as SelectList, "-- REFERENCE TYPE --")
@Html.ValidationMessageFor(x => x.reftab)
thanks in advance.
编辑:顺便说一下,下面是我的控制器中的代码为了显示下拉菜单 的值 public ActionResult Create()
{
List<CommonEntities> ISharedUI = CommonDAL.GetARSharedReference();
ViewBag.ISharedUI = new SelectList(ISharedUI, "ID", "Description");
return View("Create");
}
//Here here here here here here
// POST: ListOfItems/Create
[HttpPost]
public ActionResult Create(ListOfItems objListOfItems)
{
try
{
// TODO: Add insert logic here
List<CommonEntities> ISharedUI = CommonDAL.GetARSharedReference();
ViewBag.ISharedUI = new SelectList(ISharedUI, "ID", "Description");
ListOfItems objListOfItemss = new ListOfItems();
objListOfItemss = ARSharedDAL.CreateARSharedInsert(objListOfItems);
return RedirectToAction("Index");
}
catch
{
List<CommonEntities> ISharedUI = CommonDAL.GetARSharedReference();
ViewBag.ISharedUI = new SelectList(ISharedUI, "ID", "Description");
return View();
}
}
可以使用jQuery。给下拉列表指定一个id,如下所示。
<div id="FormDiv">
@using (Html.BeginForm("Create", "CONTROLLER", null, FormMethod.Post, new { }))
{
@Html.DropDownListFor(model => model.Reference, ViewBag.ISharedUI as SelectList, "-- REFERENCE TYPE --",new {id = "dblist" })
@Html.ValidationMessageFor(x => x.reftab)
}
</div>
然后使用jQuery提交父表单
$('#dblist').on('change', function(event){
var form = $(event.target).parents('form');
form.submit();
});