繁体   English   中英

MVC表单捕获问题

[英]MVC form capture problems

不能解决这个问题,一半可以解决一半,我不明白为什么:

有一个表格,我正在输入详细信息并检查输入错误和类似内容,但似乎只有部分数据被捕获并推回控制器。

首先,它基于此模型:

    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>

我忽略或弄乱了什么明显的原因吗?

如果您通过ID查找,您的jQuery选择器需要使用#。

首先尝试检查您要发布的数据。 另外,您不需要枚举其中的值,只需使用$(form).serializeArray() 方法即可提供表单字段的JSON数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM