简体   繁体   English

通过jQuery使用ASP.NET MVC提交表单并接收值

[英]Submitting a form with ASP.NET MVC via jQuery and receiving a value

I'm working on a ASP.NET MVC project where, upon submitting a form, the model creates a new record in the database and sends back the record ID to the form (which will then be acted upon in some additional JavaScript code). 我正在一个ASP.NET MVC项目上,在该项目中,提交表单后,模型将在数据库中创建新记录,并将记录ID发送回表单(然后将在其他一些JavaScript代码中进行处理)。

I'm using the Telerik Kendo window control, so the idea is that the user will click on the submit button in the Kendo pop-up window and will set the chain of events in motion. 我使用的是Telerik Kendo窗口控件,因此,想法是用户将在Kendo弹出窗口中单击“提交”按钮,并设置一系列活动。 Once the value has been received, the Kendo pop-up window will be closed. 接收到该值后,将关闭Kendo弹出窗口。

I have been following the example in this thread: PartialView with a form in a kendo window 我一直在关注该线程中的示例: 在Kendo窗口中使用窗体的PartialView

This behavior works fine, as far as submitting the form. 就提交表单而言,此行为效果很好。 This code does not receive any value from the controller. 该代码未从控制器接收任何值。 In the view: 在视图中:

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

And later on... 后来...

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

In the controller: 在控制器中:

[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.
    }
}

I want to grab the record ID of the record we just saved to the database and feed that back to the jQuery calling code somehow. 我想获取刚刚保存到数据库中的记录的记录ID,并以某种方式将其反馈给jQuery调用代码。 I'm able to send back the actual value of the record ID in the controller, but it will be returned as a web page that features the record ID... 我可以在控制器中发送回记录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.
    }
}

On the jQuery side, it appears that I shouldn't be using $('form').submit() since there isn't a way to get back data via a success/error mechanism. 在jQuery方面,似乎我不应该使用$('form')。submit(),因为没有一种方法可以通过成功/错误机制获取数据。 I have tried several other ways (viewbags, jQuery trigger, jQuery POST, the close mechanism on the Kendo window control) but so far I haven't been able to put all the pieces together. 我尝试了其他几种方式(视图袋,jQuery触发器,jQuery POST,Kendo窗口控件上的关闭机制),但到目前为止,我还无法将所有部分放在一起。

I suspect I am going about this in entirely the wrong way. 我怀疑我将以完全错误的方式进行此操作。 Any suggestions would be greatly appreciated! 任何建议将不胜感激!

In your controller return Json instead of Content such that: 在您的控制器中,返回Json而不是Content,这样:

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

Then in your js, use $.post such that 然后在您的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 ...
})

basically you are using jquery to post the form, that way you can get the result of post. 基本上,您使用的是jquery发布表单,这样您就可以获取发布结果。 However, you have to becareful when you don't return id (in you current code there is a possible redirect, whichi will not work) - perhaps return something that indicate there is a problem 但是,当您不返回ID时必须小心(在当前代码中可能进行重定向,这将无法正常工作)-也许返回一些表明存在问题的信息

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

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