Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a button on a page and want to pass two parameters to an action method when I click the button:

HTML
@Html.DropDownList("compId", new SelectList(@ViewBag.Comps, "Id", "Name"), "-- select Comp --", new {id="ddlComp"})

@Html.DropDownList("divId", new SelectList(@ViewBag.Div, "Id", "Name"), "-- select Division--", new {id="ddlDiv"})

<input type="button" id="btnNew" value="Add New"/>
...
<script type=text/javascript">

  $("#btnNew").click(function(){
     var compid = $("#ddlComp").val();
     var divid = $("#ddlDiv").val();
     var url = "../Admin/NewItems";

    // ? What goes here to call the Action Method?
    // When I use...
    $.get(url, {compId: compid, divId: divid});
    // the action is called but the page is not loaded
  )}


What call do I make in the script to the Action?

The Action Method...

C#
public ActionResult NewItems(string compId, string divId)
{
  // get some data... var items = db.SomeTable.Where(...);
  return View(items.ToList());
}


The Action returns a new page.

Thanks
Posted
Updated 9-May-15 14:22pm
v2

1 solution

If you want to submit to action, just using form
C#
@using (Html.BeginForm("NewItems","Admin")){
     @Html.DropDownList("compId", new SelectList(@ViewBag.Comps, "Id", "Name"), "-- select Comp --", new {id="ddlComp"})
 
     @Html.DropDownList("divId", new SelectList(@ViewBag.Div, "Id", "Name"), "-- select Division--", new {id="ddlDiv"})
     <button type="submit">Submit</button>
}

Or if you want load using jquery get, you should use callback function
JavaScript
$.get(url, {compId: compid, divId: divid}, function(data){
//load html into a div
$('#divId').html(data);
//other
});

Or you want return data using json?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900