Ajax将空值传递给控制器

时间:2018-11-26 13:12:31

标签: javascript c# asp.net asp.net-mvc asp.net-ajax

我的“ formAll”表单必须通过我的Members控制器的POST GetMember()方法接收。 此方法在参数中接收应该由ajax请求发送的模型。

问题是,当我在Web浏览器上调试时,ajax正确地序列化了我的表单,并且似乎正确地将其发送到GetMember()方法,但是metode接收了包含全为空值的模型。 ..

它曾经工作过,实际上我做了一些更改(我改变了模型),但我认为我做得正确...

PS:我特意删除了表格的一部分,以避免某些对我的问题没有附加值的行。

你有个主意吗?

@model IEnumerable<jak.formulaire.Models.MemberViewModel>

@if (Model != null)
{
    @foreach (var item in Model)
    {

            @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "formAll", style = "margin-top:3%" }))
            {
                <div id="formMember">
                    <div style="margin-bottom:5%;margin-left:5%; margin-top:-2%"><h4>Personnal</h4></div>

                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-check-inline col-12">
                        @Html.LabelFor(model => item.Member_NameFirst, "Politeness", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.EditorFor(model => item.Member_Politeness, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter politeness", @id = "Member_Politeness" } })
                        @*<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>*@
                    </div>
                    @Html.ValidationMessageFor(model => item.Member_Politeness, "", new { @class = "text-danger" })
                    <div class="form-check-inline col-12" style="margin-top:1%">
                        @Html.LabelFor(model => item.Member_NameFirst, "First name", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.EditorFor(model => item.Member_NameFirst, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter first name", @id = "Member_NameFirst" } })
                    </div>
                    <div class="form-check-inline col-12" style="margin-top:1%">
                        @Html.LabelFor(model => item.Member_NameLast, "Last name", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.EditorFor(model => item.Member_NameLast, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter last name", @id = "Member_NameLast" } })
                    </div>

                    <div class="form-check-inline col-12" style="margin-top:1%">
                        @Html.LabelFor(model => item.Company_Website, "Website", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.EditorFor(model => item.Company_Website, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter website", @id = "Company_Website" } })
                    </div>
                    <br /><br /><br />
                </div>
                <div class="col-12" style="text-align:right">
                    <button type="submit" class="btn btn-primary" id="btnEditMember">Submit</button>
                </div>
            }
        </div>
    }
}
else
{
    <div style="margin-top:2%">
        <p>An error has occurred, please log out, close your browser completely and try again.</p>
    </div>
}

@section Scripts{
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            hideCPY();
            Edit();
        });

        function hideCPY() {
            $('#formCPY').hide();
            $('#formMember').show();
        }

        function hideMember() {
            $('#formMember').hide();
            $('#formCPY').show();
        }

        function Edit() {

            $(function () {
                $("#formAll").submit(function (event) {
                    event.preventDefault();

                    var formData = $("#formAll").serialize();

                    $.ajax({
                        url: "https://localhost:44338/Members/GetMember/",
                        type: 'POST',
                        data: formData,
                        success: function (data) {
                            if (data === "success") {
                                alert("Member successfully modified");
                            }
                        },
                        error: function (error) {
                            alert('An error occurs during the process of your request');
                        }
                    });
                })
            });
        }
    </script>
}




[Authorize]
[HttpPost]
public async Task<IActionResult> GetMember(MemberViewModel model)
{
    ViewBag.sessionv = HttpContext.Session.GetInt32("idMember");
    if (ViewBag.sessionv == null)
    {
        return RedirectToAction("Index", "Home");
    }

    if (ModelState.IsValid)
    {
        FileMakerRestClient client = new FileMakerRestClient("https://fms171.hostmy.solutions", "helloJAK", userName, password);


        var toCreate = new MemberViewModel
        {
            Member_NameFirst = model.Member_NameFirst,
            Member_NameLast = model.Member_NameLast,
            Member_Politeness = model.Member_Politeness,
            Member_Mail = model.Member_Mail,
            Member_JobTitle = model.Member_JobTitle,
            Member_JobDepartment = model.Member_JobDepartment,
            Member_Phone = model.Member_Phone,
            Member_MobilePhone = model.Member_MobilePhone,
            Member_Fax = model.Member_Fax,
            Member_Skype = model.Member_Skype,
            Member_Twitter = model.Member_Twitter,
            //Member_NewsletterInt = Convert.ToInt32(model.Member_Newsletter),
            //Member_VegetarianInt = Convert.ToInt32(model.Member_Vegetarian),
            //Member_AuthorizeInt = Convert.ToInt32(model.Member_Authorize),
            Company_Address = model.Company_Address,
            Company_City = model.Company_City,
            Company_Country = model.Company_Country,
            Company_Fax = model.Company_Fax,
            Company_Name = model.Company_Name,
            //Company_NoHolderInt = Convert.ToInt32(model.Company_NoHolder),
            Company_Telephone = model.Company_Telephone,
            Company_Website = model.Company_Website,
            Company_Zip = model.Company_Zip,
            Company_VAT = model.Company_VAT

        };
        var mappedItem = _mapper.Map<Models.Members>(toCreate);
        mappedItem.Member_VegetarianInt = Convert.ToInt32(toCreate.Member_Vegetarian);
        mappedItem.Member_AuthorizeInt = Convert.ToInt32(toCreate.Member_Authorize);
        mappedItem.Member_NewsletterInt = Convert.ToInt32(toCreate.Member_Newsletter);
        mappedItem.Company_NoHolderInt = Convert.ToInt32(toCreate.Company_NoHolder);
        var results = await client.EditAsync(ViewBag.sessionv,mappedItem);

        return Ok();
    }
    else return BadRequest();
}






 public class MemberViewModel
    {
        [DataMember(Name = "member_nameFirst")] // the filemaker field to use
        public string Member_NameFirst { get; set; }

        [DataMember(Name = "member_nameLast")] // the filemaker field to use
        public string Member_NameLast { get; set; }

        [DataMember(Name = "member_politeness")] // the filemaker field to use
        public string Member_Politeness { get; set; }

        [DataMember(Name = "member_jobTitle")] // the filemaker field to use
        public string Member_JobTitle { get; set; }

        [DataMember(Name = "member_jobDepartment")] // the filemaker field to use
        public string Member_JobDepartment { get; set; }

        [DataMember(Name = "member_phone")] // the filemaker field to use
        public string Member_Phone { get; set; }

        [DataMember(Name = "member_mobile")] // the filemaker field to use
        public string Member_MobilePhone { get; set; }

        [DataMember(Name = "member_fax")] // the filemaker field to use
        public string Member_Fax { get; set; }

        public bool Member_Vegetarian { get; set; }

        [DataMember(Name = "member_vegetarian")] // the filemaker field to use
        public int Member_VegetarianInt { get; set; }

        [DataMember(Name = "member_twitter")] // the filemaker field to use
        public string Member_Twitter { get; set; }

        [DataMember(Name = "member_skype")] // the filemaker field to use
        public string Member_Skype { get; set; }

        public bool Member_Authorize { get; set; }

        [DataMember(Name = "member_authorize")] // the filemaker field to use
        public int Member_AuthorizeInt { get; set; }

        public bool Member_Newsletter { get; set; }

        [DataMember(Name = "member_newsletter")] // the filemaker field to use
        public int Member_NewsletterInt { get; set; }

        [DataMember(Name = "member_zkp")] // the filemaker field to use
        public int Member_Zkp { get; set; }

        [DataMember(Name = "member_mail")] // the filemaker field to use
        public string Member_Mail { get; set; }

        [DataMember(Name = "zkp_WEB")] // the filemaker field to use
        public int Member_Zkp_WEB { get; set; }

        [DataMember(Name = "member_picture")] // the filemaker field to use
        public string Member_Picture { get; set; }

        [DataMember(Name = "company_name")] // the filemaker field to use
        public string Company_Name { get; set; }

        [DataMember(Name = "company_address")] // the filemaker field to use
        public string Company_Address { get; set; }

        [DataMember(Name = "company_zip")] // the filemaker field to use
        public string Company_Zip { get; set; }

        [DataMember(Name = "company_city")] // the filemaker field to use
        public string Company_City { get; set; }

        [DataMember(Name = "company_country")] // the filemaker field to use
        public string Company_Country { get; set; }

        [DataMember(Name = "company_vat")] // the filemaker field to use
        public string Company_VAT { get; set; }

        public bool Company_NoHolder { get; set; }

        [DataMember(Name = "company_noHolder")] // the filemaker field to use
        public int Company_NoHolderInt { get; set; }

        [DataMember(Name = "company_telephone")] // the filemaker field to use
        public string Company_Telephone { get; set; }

        [DataMember(Name = "company_fax")] // the filemaker field to use
        public string Company_Fax { get; set; }

        [DataMember(Name = "company_website")] // the filemaker field to use
        public string Company_Website { get; set; }
    }

0 个答案:

没有答案