简体   繁体   中英

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:

 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

@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:

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

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!

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”

@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.

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.

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

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

    });
</script>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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