Dex2345
Dex2345

Reputation: 13

How to display view obtained from C# in JS?

I need to display a View obtained from ASP.NET controller method.How to do that?

[HttpGet]
  public ActionResult B(string Email, string Password)
  {
  //some code
  return View();
  }

js


function A() {             
   $.get("/Home/B", {
   "Email": document.getElementById("Email").value,
   "Password": document.getElementById("Pass").value
   },

    function (data) {
       //  ......
    });

What to write in function (data) to receive and display a returned View()?

Upvotes: 0

Views: 795

Answers (3)

M_Idrees
M_Idrees

Reputation: 2172

Although a better approach for the given scenario could be to use Partial Views. From your action method return a Partial View and call/load that action method like this:

$("#divToDisplay").load("/home/B", 
           { 
              Email: document.getElementById("Email").value
             , Password: document.getElementById("Pass").value 
            }
          );

And In this approach you are using, you just have to use the html() method to render the returned html content from view.

function A() {             
   $.get("/Home/B", {
   "Email": document.getElementById("Email").value,
   "Password": document.getElementById("Pass").value
   },

    function (data) {
        $("#divToDisplay").html(data);
    });

Where divToDisplay is the id of any div in your html content where you want to place the view's html.

Upvotes: 0

Hannan Ayub
Hannan Ayub

Reputation: 408

The 'data' returned from ajax Callback is the HTML of the view. You can show it an html element.

function A() {             
                    $.get("/Home/B", {
                        "Email": document.getElementById("Email").value,
                        "Password": document.getElementById("Pass").value   },
                     function (data) {
                         $('#div-where-you-want-to-show-view').html(data);

                     });

Without Jquery:

document.getElementById("div-where-you-want-to-show-view").innerHTML = data;

Upvotes: 1

Steven Lemmens
Steven Lemmens

Reputation: 720

If you're using jQuery, this is what you are looking for

function (resultingHtml) {
   $(".the-div-you-are-displaying-the-results-in").html(resultingHtml);
}

Upvotes: 0

Related Questions