Description:
In this example we explain that how
to return XML file from controllers action method to razor view in MVC.or how
to get response form Action method as an XML file form controller in asp.net
MVC.or how to send XML file as an JSON response from controllers action method
to view in asp.net MVCX.
Here we demonstrate how to return ContentResult
type as an XML file from Controllers action method to view in MVC.or how to
bind or populate XML file in razor view in MVC.or dynamically display an XML
file in razor view from controllers action method in mvc.
public class
EmployeeController : Controller
{
// GET:
Home
public
ActionResult Index()
{
return
View();
}
[HttpPost]
public
ContentResult AjaxMethod()
{
string
xml = System.IO.File.ReadAllText(Server.MapPath("~/XML/Employee.xml"));
// here is your XML file Path
return
Content(xml);
}
}
View:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta
name="viewport"
content="width=device-width"/>
<title>Return XML file from Controller's Action method
to View in ASP.Net MVC</title>
</head>
<body>
<input
type="button"
id="btnZFetchXMLFile"
value="Get
XML"/>
<hr/>
<textarea
id="txtdisplayXMLFile"
rows="25"
cols="40"
style="border:none; overflow:hidden"></textarea>
<script
type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script
type="text/javascript">
$(function
() {
$("#btnZFetchXMLFile").click(function () {
$.ajax({
type: "POST",
url: "/Employee/AjaxMethod",
data: '',
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (response) {
$("#txtdisplayXMLFile").val(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
});
</script>
</body>
</html>
0 comments:
Post a Comment