Reputation: 61
In my asp.net mvc 4 application i want to pass a parameter to partial view,however the parameter we want to pass is coming from javascript code
Below is the code
<script>
var TestId;
$(document).ready(function () {
// Send an AJAX request
$.getJSON("/api//GetFun?Id="[email protected],
function (data) {
TestId= data.Id
//i am getting the id here which i need to pass in partial view
}
1)...........
});
</script>
html code:
<div id="tab1" >
2).... @{ Html.RenderAction("MyPartialView", "MyController", new { id = TestId });}
</div>
So let me know how can i pass the test id to my partial view :in HTML(2) code or in javascript (1)
Upvotes: 1
Views: 14616
Reputation: 12032
View: At here we pass "category" parameter to the Controller with "admin" value
@{ Html.RenderAction("Menu", "Nav", new { category = "admin" }); }
Controller: At here we get "category" parameter's value
public PartialViewResult Menu(string category, string menu = null)
{
IEnumerable<Menu> menus = repository.Menus
.Select(x => x)
.Where(x => (x.MenuCategory == category)
.OrderBy(x => x.MenuSequence).ToList();
return PartialView(menus);
}
Hope this helps.
Upvotes: 0
Reputation: 102
Use the below code in your javascript and Load your view from javascript function
var url = '@Url.Action("MyPartialView", "MyController")';
url += '/?Id=' + TestId ;
$("#tab1").load(url);
Put the below Code in your Controller
public ActionResult MyPartialView( int id )
{
return Partial( "MyPartialView", id );
}
Hope this helps
Upvotes: 4
Reputation: 1374
From this SO question:
Assuming your controller is called "MyController", your partial view is called "MyPartialView", and you create a controller method that accepts the Id parameter and returns the partial view:
public ActionResult GetPartialView( int id )
{
return Partial( "MyPartialView", id );
}
You could then load the partial view using the below jQuery:
$('#tab1').load('/My/GetPartialView?id=' + TestId);
Upvotes: 0