Search code examples
jqueryviewpartialparameterized

how to pass parameter to partial view in MVC4 razor


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="+@ViewBag.Id,

                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)


Solution

  • 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