不能解决这个问题,有一半工作没有,我不明白为什么:
有一个表单,我正在输入详细信息并检查拼写错误和类似的事情,但只有部分数据似乎被捕获并推回到控制器。
首先,它基于这个模型:
public class First
{
public int ID { get; set; }
public string Title { get; set; }
public string Initials { get; set; }
public string Forename { get; set; }
public string MiddleName { get; set; }
public string Surname { get; set; }
public string DateOfBirth { get; set; }
public string Gender { get; set; }
public string Nationality { get; set; }
public string NiNumber { get; set; }
public decimal Al { get; set; }
public decimal RolledOver { get; set; }
public int HomePhone { get; set; }
public int MobilePhone { get; set; }
public string Email { get; set; }
public string AdName { get; set; }
public int InproNo { get; set; }
public string StartDate { get; set; }
public bool IsContractor { get; set; }
public int IsContractorInt { get; set; }
public bool IsPartTime { get; set; }
public int IsPartTimeInt { get; set; }
}
此会话变量查看模型:
public class MySessionValues
{
public First Employee ;
public Second Department;
public Fourth Checked;
// other steps here ...
}
}
这是我有一个问题的部分:
下拉框有效,但当我尝试显示时,标题会变回空白
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="textbox", id = "txtTitle" } })</div>
<div class="div-val-cell" , style="display: table-cell"> @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) </div>
姓氏有效,但DateOfBirth空无一人
<div style="display: table-row;">
<div class="div-label-text" , style="display: table-cell"> @Html.LabelFor(model => model.Surname) </div>
<div class="div-EditorFor" , style="display: table-cell"> @Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "div-form-control", id = "txtSurname" } }) </div>
<div class="div-val-cell" , style="display: table-cell"> @Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" }) </div>
<div class="div-label-text" , style="display: table-cell"> @Html.LabelFor(model => model.DateOfBirth) </div>
<div class="div-EditorFor" , style="display: table-cell"> @Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "div-form-control", id = "txtDateOfBirth" } }) </div>
<div class="div-val-cell" , style="display: table-cell"> @Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" }) </div>
</div>
即使在勾选时也始终返回false / 0:
<div class="div-label-text" , style="display: table-cell"> @Html.LabelFor(model => model.IsContractor) </div>
<div class="div-EditorFor" , style="display: table-cell"> <div>IsContractor @Html.CheckBoxFor(model => model.IsContractor, new { id = "txtIsContractor", value = 1 })</div> </div>
<div class="div-val-cell" , style="display: table-cell"> @Html.ValidationMessageFor(model => model.IsContractor, "", new { @class = "text-danger" }) </div>
<div class="div-label-text" , style="display: table-cell"> @Html.LabelFor(model => model.IsPartTime) </div>
<div class="div-EditorFor" , style="display: table-cell"> <div>@Html.CheckBoxFor(model => model.IsPartTime, new { id = "txtIsPartTime", value = 1 })</div> </div>
<div class="div-val-cell" , style="display: table-cell"> @Html.ValidationMessageFor(model => model.IsPartTime, "", new { @class = "text-danger" }) </div>
在局部视图中我有这个来捕捉值:
<script>
var urlAction = "@Url.Content("~/Treeview/_NewEmpDetails")";
function AjaxGoodRedirect(urlAction) {
console.log(urlAction);
$.ajax({
type: "POST",
url: urlAction,
data: JSON.stringify({
Title: $("#txtTitle").val()
, Initials: $("#txtInitials").val()
, Forename: $("#txtForename").val()
, MiddleName: $('#txtMiddleName').val()
, Surname: $("#txtSurname").val()
, DateOfBirth: $("txtDateOfBirth").val()
, Gender: $("txtGender").val()
, Nationality: $("txtNationality").val()
, NiNumber: $("txtNiNumber").val()
, Al: $("txtAl").val()
, RolledOver: $("txtRolledOver").val()
, HomePhone: $("txtHomePhone").val()
, MobilePhone: $("txtMobilePhone").val()
, Email: $("txtEmail").val()
, AdName: $("txtAdName").val()
, InproNo: $("txtInproNo").val()
, StartDate: $("txtStartDate").val()
, IsContractorInt: $("txtIsContractor").is(':checked') ? 1 : 0
, IsPartTimeInt: $("txtIsPartTime").is(':checked') ? 1 : 0
}),
datatype: "JSON",
contentType: "application/json; charset=utf-8",
success: function (returndata) {
if (returndata.ok)
$("#detailView").load(returndata.newurl);
else
window.alert(returndata.message);
}
}
);
}
</script>
控制器部分:
public ActionResult _NewEmpDetails()
{
IEnumerable<SelectListItem> NameTitle = new List<SelectListItem>();
using (EIPInternalEntities ctx = new EIPInternalEntities())
{
ViewBag.NameTitle = new SelectList(ctx.Database.SqlQuery<string>("EXEC dbo.uspGetLkUpTitle").ToList());
}
IEnumerable<SelectListItem> Gender = new List<SelectListItem>();
using (EIPInternalEntities ctx = new EIPInternalEntities())
{
ViewBag.Gender = new SelectList(ctx.Database.SqlQuery<string>("EXEC dbo.uspGetLkUpGender").ToList());
}
return View();
}
[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;
ViewBag.Forename = sessionValues.Employee.Forename;
ViewBag.MiddleName = sessionValues.Employee.MiddleName;
ViewBag.DateOfBirth = sessionValues.Employee.DateOfBirth;
ViewBag.Surname = sessionValues.Employee.Surname;
ViewBag.Gender = sessionValues.Employee.Gender;
ViewBag.Nationality = sessionValues.Employee.Nationality;
ViewBag.NiNumber = sessionValues.Employee.NiNumber;
ViewBag.Al = sessionValues.Employee.Al;
ViewBag.RolledOver = sessionValues.Employee.RolledOver;
ViewBag.HomePhone = sessionValues.Employee.HomePhone;
ViewBag.MobilePhone = sessionValues.Employee.MobilePhone;
ViewBag.Email = sessionValues.Employee.Email;
ViewBag.AdName = sessionValues.Employee.AdName;
ViewBag.InproNo = sessionValues.Employee.InproNo;
ViewBag.StartDate = sessionValues.Employee.StartDate;
ViewBag.IsContractor = sessionValues.Employee.IsContractor;
ViewBag.IsContractorInt = sessionValues.Employee.IsContractorInt;
ViewBag.IsPartTime = sessionValues.Employee.IsPartTime;
ViewBag.IsPartTimeInt = sessionValues.Employee.IsPartTimeInt;
return PartialView();
}
并在_NewEMpSecond
中显示调试结果@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div> Value check for Title: @ViewBag.NameTitle </div>
<div> Value check for Initials: @ViewBag.Initials </div>
<div> Value check for DateOfBirth: @ViewBag.DateOfBirth </div>
<div> Value check for Forename: @ViewBag.Forename </div>
<div> Value check for MiddleName: @ViewBag.MiddleName </div>
<div> Value check for Surname: @ViewBag.Surname </div>
<div> Value check for Gender: @ViewBag.Gender </div>
<div> Value check for Nationality: @ViewBag.Nationality </div>
<div> Value check for NiNumber: @ViewBag.NiNumber </div>
<div> Value check for Al: @ViewBag.Al </div>
<div> Value check for RolledOver: @ViewBag.RolledOver </div>
<div> Value check for HomePhone: @ViewBag.HomePhone </div>
<div> Value check for MobilePhone: @ViewBag.MobilePhone </div>
<div> Value check for Email: @ViewBag.Email </div>
<div> Value check for AdName: @ViewBag.AdName </div>
<div> Value check for InproNo: @ViewBag.InproNo </div>
<div> Value check for StartDate: @ViewBag.StartDate </div>
<div> Value check for IsContractor: @ViewBag.IsContractor </div>
<div> Value check for IsContractorInt: @ViewBag.IsContractorInt </div>
<div> Value check for IsPartTime: @ViewBag.IsPartTime </div>
<div> Value check for IsPartTimeInt: @ViewBag.IsPartTimeInt </div>
我忽略或搞砸了什么明显的理由?
答案 0 :(得分:0)
首先尝试检查您要发布的数据。此外,您不需要枚举那里的值,只需使用$(form).serializeArray()
method,它将提供表单字段的JSON数组。