简体   繁体   English

MVC 3 AJAX表格发布

[英]MVC 3 AJAX Form Posting

All, 所有,

I am trying to implement an AJAX post of a form.(New to ASP.NET MVC,coming from a Rails background) 我正在尝试实现一个表单的AJAX帖子。(ASP.NET MVC的新功能,来自Rails背景)

When I submit the form and watch the FireBug console as well as the page, the form is not doing an AJAX post. 当我提交表单并观看FireBug控制台以及页面时,表单不会执行AJAX帖子。 The AJAX handler gets invoked (Contact/Process) and the form redirects there, to Contact/Process showing just the response message, instead of updating the designated div on the form 调用AJAX处理程序(Contact / Process)并将表单重定向到那里,显示响应消息的Contact / Process,而不是更新表单上的指定div

I must be implementing something wrong some place, most likely in the controller that handles the AJAX POST. 我必须在某个地方实现错误,最有可能在处理AJAX POST的控制器中。 Not sure what else. 还不确定还有什么。

Thanks 谢谢

The Form (/Views/Contact/Create.cshtml) 表格(/Views/Contact/Create.cshtml)

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<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)


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

    }

</div>

The Controller Action (/Controllers/ContactController.cs) Controller Action(/Controllers/ContactController.cs)

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

            return "Success!";

        }

        return "Failure";

    }

You need these two libraries: 你需要这两个库:

<script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

Update 更新

Your Process action needs to return an ActionResult and use Content rather than a string. 您的Process操作需要返回ActionResult并使用Content而不是字符串。

Change it to this: 把它改成这个:

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

        return Content("Success!");

    }

    return Content("Failure");

}

您需要将jquery脚本添加到页面

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM