ajax.beginform将null值发布到控制器



我搜索了很多,但尚未找到答案。我有一个形式的部分视图:

@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>
}

此表单向我的控制器张贴,但是当我查看控制器中的内容时,所有值均为null。

我的控制器:

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");
            }
        }
    }
}

变量数据具有无效值。我有几乎相似的形式和控制器,可以处理其他一些完美工作的事情...

所以我有点迷失了,就像返回的值只是空...

的原因一样

我的模型是否有兴趣:

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; }
    }

模型绑定与 DefaultModelBinder不起作用,因为您使用的是 internal set访问者 public在某些属性中获取访问者:

public string FullName { get; internal set; }

上述定义的意思是:

  • 您应用程序中的其他代码可以读取属性值
  • 与您的应用程序在同一组装中的代码可能会将新值写入属性

由于DefaultModelBinder使用反射来对属性的后端字段执行读取和写作值(在其定义中可以看到),因此必须将get和set访问器设置为 public,以启用使用自动属性的双向模型绑定。

因此,ViewModel类中的所有属性都必须具有将访问和设置为公共的访问者,以使从HTML帮助者分配的属性值在视图中分配:

public string FullName { get; set; }

,因为您的设定器是内部标记的,所以模型粘合剂无法设置属性。

更改

 public string FullName { get; internal set; }

to

 public string FullName { get; set; }

应该解决您的问题

相关内容

  • 没有找到相关文章

最新更新