javascript ASP.NET MVC提交表单调用另一个提交表单的javascript函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13126092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC Submit form calls a javascript function of another submit form
提问by Hyman88PD
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). And the site stop working in var model = @Html.Raw(Json.Encode(Model))';, with an InvalidOperationException.
现在,问题是:当我调用第一个提交时,应该执行研究的那个,它执行研究,但它也会继续进行,调用postData()javascript 函数(即使我从未“告诉”他做那)。并且该站点停止在var model = @Html.Raw(Json.Encode(Model))'; 中工作。,带有 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. 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! 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!
嗯,问题解决了。显然,我不太了解 View & Javascript 行为。长话短说,似乎加载自身的视图进入 js 函数以某种方式缓存模型编码,但它并没有完全运行该函数!我在感兴趣的模型中遇到了问题。我不知道我是否解释过自己,但警告是:在做任何其他事情之前,请注意您的模型一致性。感谢任何帮助我的人,在我意识到我完全错了之前!
采纳答案by Yasser Shaikh
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 i.e “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 接受一个名为 stringstringButton 的输入参数,其余部分是不言自明的。
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. The 1st post to a Controller in the right way, but THEN it runs the javascript function, too.
嗨,我知道这个解决方法,我的问题是第二次提交不必通过控制器:它必须在视图中调用 javascript 函数。以正确的方式第一次发布到控制器,但它也运行了 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>
回答by Andrew Stakhov
Try changing your second submit from <input type="submit"
to <input type="button"
尝试将您的第二次提交从 更改<input type="submit"
为<input type="button"