我被困在这里一个多星期了。我正在尝试创建一个下拉列表,当用户从下拉列表中单击某个值时,它将重定向到另一个名为index的视图。以下是我的下拉代码。下拉列表中的值是从SQL Server获取的。如果我在控制器中或在视图中更改,我该怎么办?
@Html.DropDownListFor(model => model.Reference, ViewBag.ISharedUI as SelectList, "-- REFERENCE TYPE --")
@Html.ValidationMessageFor(x => x.reftab)
提前感谢。
编辑:
顺便说一句,下面是我的控制器中的代码,以显示下拉列表的值
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();
}
}
答案 0 :(得分:0)
您可以使用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();
});