Ajax.beginform将空值发布到控制器

时间:2019-03-01 08:56:20

标签: c# ajax asp.net-mvc null form-submit

我已经搜索了很多,但是还没有找到答案。 我有一个表单的局部视图:

@model PetDate.Models.ProfileDataModel

@using (Ajax.BeginForm("UpdateProfileInfo", "Profile", new AjaxOptions { HttpMethod = "Post", OnFailure = "", OnSuccess = "" }))
{
    <div class="form-group">
        @Html.LabelFor(x => x.FullName, new { @class = "text-info" })
        @Html.TextBoxFor(x => x.FullName, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(x => x.Email, new { @class = "text-info" })
        @Html.TextBoxFor(x => x.Email, new { @class = "form-control", @type = "email" })
    </div>
    <div class="form-group">
        @Html.LabelFor(x => x.BirthDay, new { @class = "text-info" })
        @Html.TextBoxFor(x => x.BirthDay, new { @class = "form-control", @type = "datetime" })
    </div>
    <div class="form-group">
        @Html.LabelFor(x => x.Username, new { @class = "text-info" })
        @Html.TextBoxFor(x => x.Username, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(x => x.AddressText, new { @class = "text-info" })
        @Html.TextBoxFor(x => x.AddressText, new { @class = "form-control", @id = "adresse" })


        @Html.HiddenFor(x => x.Postal, new { @id = "postalAddress" })
        @Html.HiddenFor(x => x.Street, new { @id = "streetAddress" })
        @Html.HiddenFor(x => x.HouseNumber, new { @id = "houseNumberAddress" })
        @Html.HiddenFor(x => x.story, new { @id = "storyAddress" })
        @Html.HiddenFor(x => x.roomPlacement, new { @id = "roomPlacementAddress" })

    </div>
    <div class="form-group">
        @Html.LabelFor(x => x.Description, new { @class = "text-info" })
        @Html.TextAreaFor(x => x.Description, new { @class = "initTinyMCE" })
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-outline-warning btn-lg" >Opdatér information</button>
    </div>
}

此表单会发布到我的控制器上,但是当我查看控制器中输入的内容时,所有值都为空。

我的控制器:

namespace PetDate.Controllers
{
    public class ProfileController : MasterController
    {

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult UpdateProfileInfo(ProfileDataModel data)
        {
            int ownerID = Convert.ToInt32(Session["userID"]);
            if (ModelState.IsValid)
            {
                using (var context = new ModelContainer())
                {
                    var ownerProfile = context.Owners.Where(x => x.ID == ownerID).FirstOrDefault();
                    var ownerLogin = context.Logins.Where(x => x.Owner_ID == ownerID).FirstOrDefault();

                    Address ownerAddress = null;

                    if (data.story != 0 && data.roomPlacement != string.Empty)
                    {
                        ownerAddress = context.Addresses.Where(x => x.Postal == data.Postal && x.Street == data.Street && x.Housenumber == Convert.ToInt32(data.HouseNumber) && x.Story == Convert.ToInt32(data.story) && x.Door == data.roomPlacement).FirstOrDefault();
                    }
                    else { 
                        ownerAddress = context.Addresses.Where(x => x.Postal == data.Postal && x.Street == data.Street && x.Housenumber == Convert.ToInt32(data.HouseNumber)).FirstOrDefault();
                    }


                    if (ownerProfile != null && ownerLogin != null)
                    {
                        // update content.
                        ownerProfile.FullName = data.FullName;
                        ownerProfile.Email = data.Email;
                        ownerProfile.Birthday = data.BirthDay;
                        ownerProfile.Description = data.Description;
                        ownerProfile.ProfilePicture = data.ProfilePicture;
                        ownerLogin.UserName = data.Username;

                        if (ownerAddress != null)
                        {   // already created the address, just get the ID
                            ownerProfile.Address_FK = ownerAddress.ID;

                        }
                        else
                        {   // has not been created before, create new one.

                            var obj = new Address()
                            {
                                Postal = data.Postal,
                                Street = data.Street,
                                Housenumber = Convert.ToInt32(data.HouseNumber),
                                Story = data.story,
                                Door = data.roomPlacement != string.Empty ? data.roomPlacement : ""
                            };

                        }


                        context.SaveChanges();
                        return new HttpStatusCodeResult(200, "ok");
                    }
                    else
                    {
                        return new HttpStatusCodeResult(400, "failure");
                    }
                }
            }
            else
            {
                return new HttpStatusCodeResult(400, "failure");
            }
        }
    }
}

变量Data,具有空值。 我有一个几乎类似的表单和控制器,可以处理其他一些正常工作的事情……

所以我有点迷茫,因为为什么返回的值只是null ...

如果有兴趣,我的模特:

public class ProfileDataModel
    {
        [Required]
        public string FullName { get; internal set; }
        [Required]
        public DateTime? BirthDay { get; internal set; }
        [Required]
        public string Description { get; internal set; }
        [Required]
        public string Email { get; internal set; }
        [Required]
        public string AddressText { get; internal set; }
        [Required]
        public string Postal { get; set; }
        [Required]
        public string Street { get; set; }
        [Required]
        public int HouseNumber { get; set; }
        public int story { get; set; }
        public string roomPlacement { get; set; }
        [Required]
        public string ProfilePicture { get; internal set; }
        [Required]
        public string Username { get; internal set; }
        public string Password { get; set; }
    }

2 个答案:

答案 0 :(得分:2)

模型绑定不适用于DefaultModelBinder,因为您正在将public的{​​{3}}设置访问器与public string FullName { get; internal set; } 的某些属性中的get访问器一起使用:

DefaultModelBinder

以上定义意味着:

  • 应用程序中的其他代码可以读取属性值
  • 只有与您的应用程序位于同一程序集中的代码可以向该属性写入新值

由于public使用反射在属性的后端字段中执行读取和写入值(您可以在internal中看到),因此get和set访问器都必须设置为public string FullName { get; set; } 才能启用具有自动属性的双向模型绑定。

因此,viewmodel类内部的所有属性都必须同时将get和set访问器定义为public,以允许从HTML帮助器分配的属性值进入视图:

java.time.LocalDate

答案 1 :(得分:1)

由于设置器被标记为内部,因此模型绑定器无法设置属性。

更改

 public string FullName { get; internal set; }

 public string FullName { get; set; }

应该解决您的问题