有一種表單將值傳遞到Ajax腳本中,然後傳遞給控制器。所有手動輸入的工作,但在下拉列表中的任何東西似乎都不會被傳回。將選定的DropDownListFor值傳遞給Ajax
因此,例如:當用戶提交表單
,我在這裏捕捉他們:
<script>
var urlAction = "@Url.Content("~/Treeview/_NewEmpDetails")";
function AjaxGoodRedirect(urlAction) {
console.log(urlAction);
var form = $("#myForm");
form.validate();
if (form.valid()) {
$.ajax({
type: "POST",
url: urlAction,
data: JSON.stringify({
Title: $("#txtTitle").val()
, Initials: $("#txtInitials").val()
這則
<div style="display: table-row;">
<div class="div-dd-label-text" , style="display: table-cell">
@Html.LabelFor(model => model.Title)
</div>
<div class="div-dropdown-menu" , style="display: table-cell">
@Html.DropDownListFor(model => model.Title, (SelectList)ViewBag.NameTitle, "Please select a title", new { htmlAttributes = new { @class = "dropdown", id = "txtTitle" } })
</div>
<div class="div-val-cell" , style="display: table-cell">
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
<div class="div-label-text" , style="display: table-cell">
@Html.LabelFor(model => model.Initials)
</div>
<div class="div-EditorFor" , style="display: table-cell">
@Html.EditorFor(model => model.Initials, new { htmlAttributes = new { @class = "div-form-control", id = "txtInitials" } })
</div>
<div class="div-val-cell" , style="display: table-cell">
@Html.ValidationMessageFor(model => model.Initials, "", new { @class = "text-danger" })
</div>
</div>
下拉列表返回標題的列表擊中此:
[HttpPost]
public ActionResult _NewEmpDetails(NewEmp.First model)
{
if (ModelState.IsValid)
{
var sessionValues = new MySessionValues();
sessionValues.Employee = model;
Session["MySessionValues"] = sessionValues;
}
return Json(new { ok = true, newurl = ("/Treeview/_NewEmpSecond") }, "application/json", JsonRequestBehavior.DenyGet);
}
重定向到一個表單
public ActionResult _NewEmpSecond()
{
var sessionValues = Session["MySessionValues"] as MySessionValues;
ViewBag.NameTitle = sessionValues.Employee.Title;
ViewBag.Initials = sessionValues.Employee.Initials;
return PartialView();
}
目前我設置ViewBag
屬性是用於調試的值,並在首字母都有一個值進入nameTitle不,我不能明白爲什麼
我有這個在未來的形式,現在顯示的值:
<div> Value check for Title: @ViewBag.NameTitle </div>
<div> Value check for Initials: @ViewBag.Initials </div>
任何人都可以看到我在哪裏呢?
你錯過了「<」char?在div class =「div-dropdown-menu」 –