c# - Ajax.beginform 将空值发布到 Controller

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

我已经搜索了很多但还没有找到答案.. 我有一个表单的部分 View :

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

此表单发布到我的 Controller ,但当我查看我在 Controller 中获得的内容时,所有值都显示为空。

我的 Controller :

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

变量数据具有空值。 我有一个几乎相似的表单和 Controller ,它可以处理其他一些完美运行的事情......

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

如果有兴趣,我的模型:

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

最佳答案

模型绑定(bind)不适用于 DefaultModelBinder,因为您使用的是 internal使用 public 设置访问器在某些属性中获取访问器,如下所示:

public string FullName { get; internal set; }

以上定义的意思是:

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

因为 DefaultModelBinder 使用反射来执行读取和写入属性后端字段的值(您可以在 its definition 中看到),所以 get 和 set 访问器都必须设置为 public 以启用具有自动属性的双向模型绑定(bind)。

因此,viewmodel 类中的所有属性都必须将 get 和 set 访问器都定义为公共(public)的,以便让属性值从 HTML 助手分配到 View 中:

public string FullName { get; set; }

关于c# - Ajax.beginform 将空值发布到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54941060/

相关文章:

c# - 将枚举映射到 "sub-enum"

c# - 从 startup.cs asp.net core 重定向用户

ajax - 如何异步上传图片

PHP AJAX HTML : changing unique table data in foreach loop

asp.net-mvc - Windows Server 2008 IIS 7.5最大文件接受限制

c# - 如何显示变量内的 Razor 帮助程序代码?

c# - .Net Windows 服务崩溃并出现 MSVCR120_CLR0400.dll 异常

c# - MonoMac WebView - 显示控制台或 Web 检查器?

javascript - AJAX 不发送变量写入 txt

c# - 如何从 ASP.NET MVC RC1 中的 ViewResult 获取模型数据?