Reputation: 13
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
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
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
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