Html.RenderAction using AJAX

Is it possible to use HTMl.RenderAction with ajax to provide a parameter?

I have a controller action like this

[ChildActionOnly] Public ActionResult EmployeeData(string id) { Employee employee = new Employee(); //do work to get data Return PartialView(employee); } 

A partial view is a small table with some data about the employee (name, address, etc.)

Then I have a page with a dropdown list of employees with the data field being the identifier required by EmployeeData (row identifier)

I would like to use ajax, so when an employee is selected from the drop-down list, a partial view of EmployeeData will be displayed below it without refreshing the page. Then again if another employee is selected.

Although I'm not sure how to do this, if possible.


As recommended, this is what I have now. (please don’t mind that this is not an example of employee data, which I mentioned above, that the data is not ready in the database, and I have several areas that will do the same, so I decided to work on it today)

here is my js in my opinion

  $("#repList").change(function () { var id = $("#repList").val(); $.ajax({ url: '/Reporting/GetSalesTargets/' + id, success: function (result) { $("#partialdiv").html(result); }, error: function () { alert("error"); } }); }); 

I get a controller action that will return the view, here it is.

 public ActionResult GetSalesTargets(string id) { int month = DateTime.Now.Month; SalesMarketingReportingService mktSrv = new SalesMarketingReportingService(); SalesTargetModel model = mktSrv.GetRSMSalesTargetModel(id, month); return PartialView(model); } 
+6
c # ajax asp.net-mvc renderaction
source share
1 answer

Perhaps, but you should remove the [ChildActionOnly] attribute. This becomes a normal action that returns a partial view that you could use with AJAX:

 $.ajax({ url: '/home/employeedata/123', success: function(result) { $('#somedivid').html(result); } }); 
+16
source share

All Articles