asp.net mvc - why when my action returns a view - it passes first through a javascript function -


in index view have button javascript function when it's clicked:

<script>     function addcat() {         $.ajax({             type: "get",             url: '@url.action("addcategory")',             data: {},         });     } </script>  @html.dropdownlist("languages", new selectlist(model.lstlanguages, "languageid", "name", session["langid"] ?? "1"), new { id = "ddllanguages" }) <div id="categoriesplace"></div> <input type="button" id="btnaddcategory" onclick="addcat()" value="addcategory" /> 

when click on button, see should redirected addcategory action, adds record in database , returns addcategory view. problem when reaches drop-down in addcategory view, goes straight addbtn_click() function if it's clicked , redirects me index action. can explain behavior?

so, here categorycontroller:

public class categorycontroller : controller {     public actionresult addcategory()     {         categoryviewmodel vm = new categoryviewmodel();         vm.addnewcategory();         return view(vm);     }      public actionresult addcategorieslanguages(int catid, int lanid, string title, string shrtdescription, string description)     {         categoryviewmodel vm = new categoryviewmodel();         vm.addcategorieslanguages(catid, lanid, title, shrtdescription, description);         return redirecttoaction("index");      } } 

and, here addcategory view:

@model onion.web.viewmodels.categoryviewmodel  @{     viewbag.title = "addcategory"; }   <h2>addcategory</h2> @html.dropdownlist("languages", new selectlist(model.lstlanguages, "languageid", "name",@httpcontext.current.session["langid"]),new { id = "ddllanguages" }) <br /> <label for="txbtitle">title:</label> <input type="text"  id="txbtitle"/>  <br /> <label for="txbshortdescription">short description:</label> <input type="text"  id="txbshortdescription" />  <br />  <br /> <input type="button" id="btnadd" onclick="btnadd_click" value="add" />  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>   <script>     function btnadd_click() {             $.ajax({             type: "get",             url: '@url.action("addcategorieslanguages")' + '?catid=' +@model.newcategoryid +'&lanid=' + $("#ddllanguages").val() + '&title=' + $('#txbtitle').val() + '&shrtdescription=' + $('#txbshortdescription').val() + '&description=' + $('#txbdescription').val(),             data: {}         });     } </script> 

in addcategory view, try changing button's onclick attribute include function name , parenthesis shown below:

<input type="button" id="btnadd" onclick="btnadd_click()" value="add" /> 

i not 100% sure if issue, stood out me.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -