简体   繁体   English

ASP.NET MVC提交表单调用另一个提交表单的javascript函数

[英]ASP.NET MVC Submit form calls a javascript function of another submit form

big problem here! 这里有大问题! I know the title is kinda fuzzy, but it's all day long I've got this problem and cannot figure out how to solve it. 我知道标题有点模糊,但是整天我都遇到了这个问题,无法解决。 I'll try to be the more specific in the less messy way. 我将以更简洁的方式尝试变得更加具体。 Long story short, I have a Controller (LeadController) with a method (Search) for a search: 长话短说,我有一个带有搜索方法(搜索)的控制器(LeadController):

 public ActionResult Search(string prov = null, string city = null)
    {
        //if no field is compiled, return an empty view
        if (String.IsNullOrWhiteSpace(prov) && String.IsNullOrWhiteSpace(city))
            return View();

        var leads = from l in db.Leads
                    select l;

        if (!String.IsNullOrWhiteSpace(prov))
        {
            leads = leads.Where(l => l.Prov == prov).OrderBy(l => l.RagioneSoc);
        }

        if (!String.IsNullOrWhiteSpace(city))
        {
            leads = leads.Where(l => l.Comune == city).OrderBy(l => l.RagioneSoc);
        }

        return View(leads);
    }

Then I have the Search View (displaying the fields to fill for the search AND the result after the post action) with 2 submit forms: the first one to execute the search 然后,我有2个提交表单的搜索视图(显示要填充搜索的字段以及发布操作后的结果):第一个执行搜索的表单

@using (Html.BeginForm()){
Sigla Provincia: @Html.TextBox("prov", null, new { @style = "width: 50px;"})
Città: @Html.TextBox("city", null, new { @style = "width: 150px;" })
<input type="submit" value="Ricerca" data-icon="search" data-role="button" data-mini="true" data-inline="true" />}

and the 2nd one to generate a document from the leads result of the search action 第二个从搜索动作的潜在客户结果生成文档

<input type="submit" title="create doc" value="Create document" onclick="javascript:postData()" id="doc" />

This last submit should call a javascript function to encode the model: 最后一次提交应调用javascript函数对模型进行编码:

<script type="text/javascript">
function postData() {
    var urlact = '@Url.Action("createDoc")';
    var model = '@Html.Raw(Json.Encode(Model))';

    $.ajax({
        ...
        ...
    });
}
</script>

Now, the problem is: when I call the first submit, the one which should execute the research, it performs the research but it also keeps going on, calling the postData() javascript function (even if I never "tell" him to do that). 现在的问题是:当我调用第一个提交时,应该执行研究的提交,它执行研究,但是它仍然继续进行,调用postData() javascript函数(即使我从不“告诉”他去做)那)。 And the site stop working in var model = @Html.Raw(Json.Encode(Model))'; 并且该站点停止在var model = @ Html.Raw(Json.Encode(Model))'中工作; , with an InvalidOperationException. ,带有InvalidOperationException。

If someone understood my question, is there a way to avoid this behaviour and to force the 1st submit only to pass the controller the parameters to execute the search action? 如果有人理解了我的问题,是否有一种方法可以避免这种行为,并强制第一次提交仅向控制器传递参数以执行搜索操作?

Hope someone can help me, thank you in advance for your consideration! 希望有人可以帮助我,在此先感谢您的考虑!


SOLVED 解决了

Well, the problem is gone. 好了,问题解决了。 Apparently, I didn't know so well the View & Javascript behaviour. 显然,我对View&Javascript行为不太了解。 Long story shirt, it seems the View, loading itself, enters the js function in order to kind of cache the Model encoding, but it doesn't fully runs the function! 长话短说,看来,加载自身的View进入了js函数,以便某种程度上缓存Model编码,但是并不能完全运行该函数! And I had a problem within the interested Model. 我对感兴趣的模型有问题。 I don't know if I explained myself, but the warning is: be careful for your Model consistency before doing anything else. 我不知道自己是否在解释自己,但是警告是:在执行其他任何操作之前,请注意模型的一致性。 Thanks to anyone who helped me, before I realized I get it totally wrong! 感谢任何帮助过我的人,在我意识到我完全错了之前!

Here is how you should handle multiple submit buttons 这是您应该如何处理多个提交按钮的方法

Below is a form with two submit buttons. 下面是带有两个提交按钮的表单。 Note that both these submit buttons have the same name ie “submitButton” 请注意,这两个提交按钮的名称相同,即“ submitButton”

@Html.BeginForm("MyAction", "MyController"); %>
<input type="submit" name="submitButton" value="Button1" />
<input type="submit" name="submitButton" value="Button2" />
}

Now over to the Controller, the Action takes in an input parameter called string stringButton and the rest is pretty self-explanatory. 现在到Controller,Action接受一个名为string stringButton的输入参数,其余参数很不言自明。

public ActionResult MyAction(string submitButton) {
        switch(submitButton) {
            case "Button1":
               // do something here
            case "Button2":
               // do some other thing here
            default:
                // add some other behaviour here
        }
...
}

Hope this helps you ! 希望这对您有帮助!

UPDATE : 更新:

from your comments, 从您的评论中,

Hi, I do know this workaround, my issue is the 2nd submit doesn't have to pass through the Controller: it has to call the javascript function in the View. 嗨,我确实知道这种解决方法,我的问题是第二次提交不必通过Controller:它必须在View中调用javascript函数。 The 1st post to a Controller in the right way, but THEN it runs the javascript function, too. 第1条以正确的方式发布到Controller,但随后它也运行javascript函数。

Ok, instead of having two submit buttons, you can have one submit button and other as a normal button. 好的,除了有两个提交按钮,您还可以有一个提交按钮,而另一个有一个普通按钮。 Eg: - 例如:-

@Html.BeginForm("MyAction", "MyController"); %>
<input type="submit" name="submitButton" value="Button1" />
<input type="button" id="otherButton" value="Button2" />
}

and then using some simple jquery, you can make a call to the javascript funcion postData() . 然后使用一些简单的jquery,可以调用javascript postData()

<script type="text/javascript">
    $("#otherButton").bind("click", function () {
        console.log("do your stuff here");
        postData();

    });
</script>

尝试将第二个提交从<input type="submit"更改为<input type="button"

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

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