【问题标题】:Submitting a form with ASP.NET MVC via jQuery and receiving a value通过 jQuery 使用 ASP.NET MVC 提交表单并接收值
【发布时间】:2015-08-11 20:58:24
【问题描述】:

我正在开发一个 ASP.NET MVC 项目,在该项目中,提交表单后,模型会在数据库中创建一条新记录并将记录 ID 发送回表单(然后将在一些额外的 JavaScript 中对其进行操作代码)。

我正在使用 Telerik Kendo 窗口控件,因此其想法是用户将单击 Kendo 弹出窗口中的提交按钮并设置事件链。收到值后,Kendo 弹出窗口将关闭。

我一直在关注这个帖子中的例子:PartialView with a form in a kendo window

就提交表单而言,此行为可以正常工作。此代码不会从控制器接收任何值。在视图中:

<input type="button" value="Create" class="btn btn-default" onclick="formSubmit()"/>

后来……

<script>
    function formSubmit() {
    if (!inIframe()) {
        // We are a parent window
        $('form').submit();
    }
    else {
        // We are a pop-up window
        $('form').submit();
        parent.$('#window').data('kendoWindow').close();
    }
    }
</script>

@(Html.Kendo().Window()
    .Name("window")
    .Title("Attached Report")
    .Content("Loading...")
    .Actions(actions => actions.Close())
    .Iframe(true)
    .Height(700)
    .Width(1000)
    .Modal(true)
    .AutoFocus(true)
    .Visible(false)
)

在控制器中:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(VMMyViewModel viewModel)
{
    // Code omitted...basically, we save to the database
    db.SaveChanges();

    if (viewModel.IsInPopupWindow)
    {
     return null; // We return null to help us exit out of the popup window.
    }
    else
    {
    return RedirectToAction("Index"); // Since we're in the main window, go back to the index after submitting.
    }
}

我想获取我们刚刚保存到数据库中的记录的记录 ID,并以某种方式将其反馈给 jQuery 调用代码。我可以在控制器中发回记录 ID 的实际值,但它将作为包含记录 ID 的网页返回...

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(VMMyViewModel viewModel)
{
    // Code omitted...basically, we save stuff
    db.SaveChanges();

    if (viewModel.IsInPopupWindow)
    {
            return Content(reportHeader.Id.ToString());
    }
    else
    {
    return RedirectToAction("Index"); // Since we're in the main window, go back to the index after submitting.
    }
}

在 jQuery 方面,我似乎不应该使用 $('form').submit() 因为没有办法通过成功/错误机制取回数据。我尝试了其他几种方法(viewbags、jQuery trigger、jQuery POST、Kendo 窗口控件上的关闭机制),但到目前为止我还无法将所有部分放在一起。

我怀疑我的处理方式完全错误。任何建议将不胜感激!

【问题讨论】:

  • 您目前所做的只是提交一个标准表单,该表单将执行重定向。您需要使用 ajax 发布表单数据并将 ID 值作为 JsonResult 返回
  • 你能显示一些关于你的表单的代码吗?
  • 我猜它可能是 iframe。我看到你剑道窗口有一个选项 iframe(true),这是否意味着 iframe 中的表单?iframe 就像另一个窗口,在你的主页中,很难了解 js 在 iframe 中的作用

标签: jquery asp.net-mvc kendo-asp.net-mvc


【解决方案1】:

在你的控制器中返回 Json 而不是 Content 这样:

if (viewModel.IsInPopupWindow)
{
        return Json(reportHeader.Id.ToString());
}

然后在你的 js 中,使用 $.post 这样

function formSubmit() {
    if (!inIframe()) {
        // We are a parent window
        // $('form').submit();
    $.post($('form').attr("action"), $("form").serialize(), function(id){
    // you got the id

   // rest omitted ...
})

基本上你是使用 jquery 来发布表单,这样你就可以获得发布的结果。但是,当您不返回 id 时,您必须小心(在您当前的代码中,可能存在重定向,这将不起作用) - 可能会返回表明存在问题的内容

【讨论】:

  • 太棒了——这个答案就是我想要的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2016-01-11
  • 1970-01-01
相关资源
最近更新 更多