简体   繁体   English

C#.NET MVC Ajax表单提交

[英]C#.NET MVC Ajax Form Submission

Feel like I'm missing something simple here - When I submit this form it takes me to a white page with my encoded JSON model, rather than staying on the page I was on. 感觉我在这里缺少一些简单的东西 - 当我提交这个表单时,它会带我到我的编码JSON模型的白页,而不是留在我所在的页面上。

Form in a partial view, loaded on every page: 在部分视图中的表单,加载在每个页面上:

@using (Ajax.BeginForm("Inquiry", "Home", new AjaxOptions { HttpMethod = "POST", OnSuccess = "success" }))

And my actions: 我的行为:

    public ActionResult Inquiry()
    {
        return PartialView("_Inquiry",new Inquiry());
    }

    [HttpPost]
    public JsonResult Inquiry(Inquiry model)
    {
        if (ModelState.IsValid)
        {
            db.Inquiries.Add(model);
            db.SaveChanges();
        }
        return Json(model);
    }

Make sure you have referenced the following script in your page: 确保您在页面中引用了以下脚本:

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

It is what makes the Ajax.* helpers work. 这是使Ajax.*助手工作的原因。

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

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