Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Submit form calls a javascript function of another submit form
    primarykey
    data
    text
    <p>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:</p> <pre><code> public ActionResult Search(string prov = null, string city = null) { //if no field is compiled, return an empty view if (String.IsNullOrWhiteSpace(prov) &amp;&amp; String.IsNullOrWhiteSpace(city)) return View(); var leads = from l in db.Leads select l; if (!String.IsNullOrWhiteSpace(prov)) { leads = leads.Where(l =&gt; l.Prov == prov).OrderBy(l =&gt; l.RagioneSoc); } if (!String.IsNullOrWhiteSpace(city)) { leads = leads.Where(l =&gt; l.Comune == city).OrderBy(l =&gt; l.RagioneSoc); } return View(leads); } </code></pre> <p>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</p> <pre><code>@using (Html.BeginForm()){ Sigla Provincia: @Html.TextBox("prov", null, new { @style = "width: 50px;"}) Città: @Html.TextBox("city", null, new { @style = "width: 150px;" }) &lt;input type="submit" value="Ricerca" data-icon="search" data-role="button" data-mini="true" data-inline="true" /&gt;} </code></pre> <p>and the 2nd one to generate a document from the leads result of the search action</p> <pre><code>&lt;input type="submit" title="create doc" value="Create document" onclick="javascript:postData()" id="doc" /&gt; </code></pre> <p>This last submit should call a javascript function to encode the model:</p> <pre><code>&lt;script type="text/javascript"&gt; function postData() { var urlact = '@Url.Action("createDoc")'; var model = '@Html.Raw(Json.Encode(Model))'; $.ajax({ ... ... }); } &lt;/script&gt; </code></pre> <p>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 <em>postData()</em> javascript function (even if I never "tell" him to do that). And the site stop working in <strong>var model = @Html.Raw(Json.Encode(Model))';</strong>, with an InvalidOperationException.</p> <p>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?</p> <p>Hope someone can help me, thank you in advance for your consideration!</p> <hr> <p>SOLVED</p> <p>Well, the problem is gone. Apparently, I didn't know so well the View &amp; 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!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload