C# MVC表单捕获问题
无法解决这个问题,一半有效,一半无效,我不明白为什么: 有一个表单,我正在输入详细信息,并检查拼写错误和类似的事情,但似乎只有部分数据被捕获并推回到控制器 所以首先它是基于这个模型的:C# MVC表单捕获问题,c#,jquery,asp.net-mvc,C#,Jquery,Asp.net Mvc,无法解决这个问题,一半有效,一半无效,我不明白为什么: 有一个表单,我正在输入详细信息,并检查拼写错误和类似的事情,但似乎只有部分数据被捕获并推回到控制器 所以首先它是基于这个模型的: public class First { public int ID { get; set; } public string Title { get; set; } public string Initials { get; set; } public string For
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>
div class=“div dd label text”,style=“display:table cell”>@Html.LabelFor(model=>model.Title)
@Html.DropDownListFor(model=>model.Title,(SelectList)ViewBag.nametTitle,“请选择一个标题”,新建{htmlAttributes=new{@class=“textbox”,id=“txtTitle”})
@Html.ValidationMessageFor(model=>model.Title,“,new{@class=“text danger”})
姓氏有效,但出生日期为空
<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>
@Html.LabelFor(model=>model.姓氏)
@Html.EditorFor(model=>model.namite,new{htmlAttributes=new{@class=“div form control”,id=“txtnamname”})
@Html.ValidationMessageFor(model=>model.namname,“,new{@class=“text danger”})
@LabelFor(model=>model.DateOfBirth)
@EditorFor(model=>model.DateOfBirth,new{htmlAttributes=new{@class=“div form control”,id=“txtDateOfBirth”})
@Html.ValidationMessageFor(model=>model.DateOfBirth,“,new{@class=“text danger”})
即使勾选,两者都始终返回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>
@Html.LabelFor(model=>model.IsContractor)
IsContractor@Html.CheckBoxFor(model=>model.IsContractor,new{id=“txtiContractor”,value=1})
@Html.ValidationMessageFor(model=>model.IsContractor,“,new{@class=“text danger”})
@LabelFor(model=>model.IsPartTime)
@CheckBoxFor(model=>model.IsPartTime,new{id=“txtIsPartTime”,value=1})
@Html.ValidationMessageFor(model=>model.IsPartTime,“,new{@class=“text danger”})
在局部视图中,我有以下内容来捕获值:
<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>
var urlAction=“@Url.Content”(~/Treeview/_NewEmpDetails”);
函数AjaxGoodRedirect(urlAction){
console.log(urlAction);
$.ajax({
类型:“POST”,
url:urlAction,
数据:JSON.stringify({
标题:$(“#txtTitle”).val()
,首字母:$(“#txtInitials”).val()
,名称:$(“#txtForename”).val()
,MiddleName:$('#txtmidleName').val()
,姓氏:$(“#txt姓氏”).val()
,出生日期:$(“txtDateOfBirth”).val()
,性别:$(“txtGender”).val()
,国籍:$(“txtNationality”).val()
,NiNumber:$(“txtninnumber”).val()
,Al:$(“txtAl”).val()
,RolledOver:$(“txtRolledOver”).val()
,HomePhone:$(“txtHomePhone”).val()
,MobilePhone:$(“txtMobilePhone”).val()
,电邮:$(“txtEmail”).val()
,AdName:$(“txtAdName”).val()
,InproNo:$(“txtInproNo”).val()
,StartDate:$(“txtStartDate”).val()
,isContractPrint:$(“txtIsContractor”)。是否(“:选中”)?1:0
,IsPartTimeInt:$(“txtIsPartTime”)。是否(“:选中”)?1:0
}),
数据类型:“JSON”,
contentType:“应用程序/json;字符集=utf-8”,
成功:函数(返回数据){
if(returndata.ok)
$(“#详细视图”).load(returndata.newurl);
其他的
窗口。警报(返回数据。消息);
}
}
);
}
控制器部分:
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();
}
public ActionResult\u NewEmpDetails()
{
IEnumerable name title=新列表();
使用(EIPInteralenties ctx=新EIPInteralenties())
{
ViewBag.nametTitle=new SelectList(ctx.Database.SqlQuery(“EXEC dbo.uspGetLkUpTitle”).ToList();
}
IEnumerable Gender=新列表();
使用(EIPInteralenties ctx=新EIPInteralenties())
{
ViewBag.Gender=new SelectList(ctx.Database.SqlQuery(“EXEC dbo.uspGetLkUpGender”).ToList();
}
返回视图();
}
[HttpPost]
公共行动结果_NewEmpDetails(NewEmp.First模型)
{
if(ModelState.IsValid)
{
var sessionValues=new MySessionValues();
sessionValues.Employee=model;
会话[“MySessionValues”]=会话值;
}
返回Json(new{ok=true,newurl=(“/Treeview/_newempsond”)},“application/Json”,JsonRequestBehavior.DenyGet);
}
公共行动结果(newempsond)
{
var sessionValues=会话[“MySessionValues”]作为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.姓氏=sessionValues.Employee.姓氏;
ViewBag.Gender=sessionValues.Employee.Gender;
ViewBag.national=sessionValues.Employee.national;
ViewBag.NiNumber=sessionValues.Employee.NiNumber;
ViewBag.Al=sessionValues.Employee.Al;
ViewBag.RolledOver=sessionValues.Employee.RolledOver;
ViewBag.HomePhone=sessionValues.Employee.HomePho
@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>