MVC 3 - 模型在AJAX控制器中始终为空

时间:2013-12-18 17:39:35

标签: ajax asp.net-mvc asp.net-mvc-3

所有

通过AJAX将表单值发布到我的控制器(/ Contact / Process)时,模型始终为null,ModelState.IsValid始终为false。不知道为什么模型会出现空值。 AJAX帖子正在运行,并且有水合参数。

由于

萤火
参数
Contact.Email xxx@example.com
Contact.FirstName约翰
Contact.LastName Doe
Contact.Message你好 X-Requested-With XMLHttpRequest
联系提交

视图模型

namespace GuitarCollector.Models.ViewModels
{
    public class ContactViewModel
    {
        public Contact Contact { get; set; }
    }
}

模型

namespace GuitarCollector.Models
{
    public class Contact
    {
        //Primary Key
        public int Id { get; set; }

        [Display(Name = "First name")]
        [Required(ErrorMessage = "Please enter your first name")]
        public string FirstName { get; set; }

        [Display(Name = "Last name")]
        [Required(ErrorMessage = "Please enter your last name")]
        public string LastName { get; set; }



        [Display(Name = "Email address")]
        [Required(ErrorMessage = "Please enter your email address")]
        public string Email { get; set; }

        [Display(Name = "Message")]
        [Required(ErrorMessage = "Please enter your message")]
        public string Message { get; set; }

        public DateTime DateCreated { get; set; }
    }
}

表格

<div id="content">
    <div id="Status"></div>
    @using (Ajax.BeginForm("Process","Contact", new AjaxOptions{ HttpMethod = "POST", UpdateTargetId = "Status"}))
    {
         @Html.ValidationSummary(true)

         <p><span class="required">&#42; All fields are required.</span></p>

        <!-- First Name -->
        <div>
            @Html.LabelFor(model => model.Contact.FirstName) <span class="required">&#42;</span>
        </div>
        @Html.EditorFor(model => model.Contact.FirstName)
        @Html.ValidationMessageFor(model =>model.Contact.FirstName)

        <!-- Last Name -->
         <div>
            @Html.LabelFor(model => model.Contact.LastName) <span class="required">&#42;</span>
        </div>
        @Html.EditorFor(model => model.Contact.LastName)
        @Html.ValidationMessageFor(model =>model.Contact.LastName)

        <!-- Email -->
         <div>
            @Html.LabelFor(model => model.Contact.Email) <span class="required">&#42;</span>
        </div>
        @Html.EditorFor(model => model.Contact.Email)
        @Html.ValidationMessageFor(model =>model.Contact.Email)

        <!-- Message -->
         <div>
            @Html.LabelFor(model => model.Contact.Message) <span class="required">&#42;</span>
        </div>
        @Html.TextAreaFor(model => model.Contact.Message)
        @Html.ValidationMessageFor(model =>model.Contact.Message)

        <div>
            <input type="submit" name="contact" id="contact" value="Submit" class="btn"/> 
       </div>

    }

</div>

控制器

[HttpPost]
public ActionResult Process(ContactViewModel model)
{
    if (ModelState.IsValid)
    {
        model.Contact.DateCreated = DateTime.Now;
        db.Contacts.Add(model.Contact);
        db.SaveChanges();

        return Content("Success!");

    }

    return Content("Failure");

}

2 个答案:

答案 0 :(得分:2)

您必须为submit输入提供一些其他名称,而不是ViewModel的属性名称,或者根本不删除它。我想你实际上并不需要它。在这种情况下,它不会被序列化并传递给控制器​​。

模型将变为null,因为模型绑定器无法将值“Submit”绑定到Contact属性。

答案 1 :(得分:1)

你忘记了:

 @Html.HiddenFor(model => model.Contact.Id )
 @Html.HiddenFor(model => model.Contact.DateCreated)

asp.net mvc无法进行绑定