Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to POST/GET data from jQuery to MVC 4 controller and return View
    primarykey
    data
    text
    <p>So, I have a table with information on a customer, including their name, phone, email, etc. I set up the table to have a radio button to select a customer from this table. This radio-button calls a jQuery function that gets the customer's name from a controller method. I'm looking for the proper and best way to pass both the CustomerID and CustomerName to a CustomerEvent controller which returns a view of events for the specific customer. If I try to use an AJAX POST, it does not return the View in the <code>data</code>. I've also tried window.location and that does not work either. </p> <p>Here's the razor code:</p> <pre><code>@Html.ActionLink("Events ", "../CustomerEvent/Index", null, new { id = "btnEventsCustomerID" }) </code></pre> <p>jQuery: </p> <pre><code>jQuery('input[type=radio]').change(function () { jQuery('#hdnCustomerID').val($(this).val()); var CustomerID = jQuery('#hdnCustomerID').val(); var CustomerName = null; $.ajax({ url: '/Customer/GetCustomerName', data: { id: CustomerID }, type: "GET", async: false, cache: false, success: function (data) { CustomerName = data } }); alert(CustomerName); jQuery('#btnEventsCustomerID').click(function () { window.location = "/CustomerEvent/Index/?CustomerID=" + CustomerID + "&amp;CustomerName=" + CustomerName; }); </code></pre> <p>The CustomerName alert is functioning properly. Here is my controller method for returning a view of Customer events:</p> <pre><code>public ActionResult Index(int CustomerID = 0, string CustomerName="") { if (CustomerID != 0) { Session["CustomerID"] = CustomerID; Customer CustomerObj = _customer.GetCustomer(CustomerID); Session["CustomerName"] = CustomerObj.FirstName + " "+ CustomerObj.LastName; } return View(_customerEvent.GetCustomerEventCustomList(CustomerID)); } </code></pre>
    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.
 

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