我如何从下拉列表与jquery的值
本文关键字:jquery 的值 下拉列表 | 更新日期: 2023-09-27 18:18:25
控制器代码:
private UBADBEntities db = new UBADBEntities();
public ActionResult Index()
{
ViewBag.Pos1 = new SelectList(db.Positions, "posID", "posName");
ViewBag.Pos2 = new SelectList(db.Positions, "posID", "posName");
var applications = db.Applications.Include("Position").Include("Position1");
return View(applications);
}
[HttpPost]
public ActionResult Index(int id, int listPos1, int listPos2)
{
var applicationQuery =
from app in db.Applications
where app.CandidateID == id
select app;
ViewBag.Pos1 = new SelectList(db.Positions, "posID", "posName", listPos1);
ViewBag.Pos2 = new SelectList(db.Positions, "posID", "posName", listPos2);
foreach (var application in applicationQuery)
{
if (application.CandidateID == id)
{
application.Pos1 = listPos1;
application.Pos2 = listPos2;
}
}
db.SaveChanges();
return View(db.Applications.ToList());
}
视图代码:
<div id="firstPos-@item.CandidateID.ToString()" class="show">@Html.DisplayFor(modelItem => item.Position.posName)</div>
<div id="listPos1-@item.CandidateID.ToString()" class="invisible">@Html.DropDownList("Pos1", String.Empty)</div>
@Html。DisplayFor(modelItem => item.Position1.posName)<div id="listPos2-@item.CandidateID.ToString()" class="invisible">@Html.DropDownList("Pos2", String.Empty)</div>
JS脚本:
function ShowEdit(par) {
$("#listPos1-" + par).attr("class", "show");
$("#secondPos-" + par).attr("class", "invisible");
$("#listPos2-" + par).attr("class", "show");
$("#btnEdit-" + par).attr("class", "btn-toolbar text-center btn-group btn hide");
$("#btnSave-" + par).attr("class", "btn-toolbar text-center btn-group btn show");
}
function SaveEdit(par) {
$("#listPos1-" + par).attr("class", "invisible");
$("#secondPos-" + par).attr("class", "show");
$("#listPos2-" + par).attr("class", "invisible");
$("#btnEdit-" + par).attr("class", "btn-toolbar text-center btn-group btn show");
$("#btnSave-" + par).attr("class", "btn-toolbar text-center btn-group btn hide");
var _listPos1 = $("#listPos1-" + par).val();
var _listPos2 = $("#listPos2-" + par).val();
var url = '@Url.Action("Index","Application")';
$.post(url, { id: par listPos1: _listPos1, listPos2: _listPos2 }, function (data) {
$("#divResult").html(data);
});
}
尝试使用下面的代码,这可能会对您有所帮助。
$('#<%=yourID.ClientID %> option:selected').val()
检查你的文章语法。少了一个逗号
$.post(url, { id: par , listPos1: _listPos1, listPos2: _listPos2 }, function (data) {
$("#divResult").html(data);
});