PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] how to pass parameter to partial view in MVC4 razor

 May 18, 2022     jquery, parameterized, partial, view     No comments   

Issue

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



Answered By - user1799982
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing