Passing html string using ajax post in MVC,
Razor
Description:
Passing html string using ajax post in MVC. Below given example shows use of
Ajax function to submit html content
(Note:
[ValidateInput(false)]
is important attribute that should be used on Controller side method for
submission of html contents )
ViewPageName: Test.cshtml
<script type="text/javascript"
language="javascript">
$("#btnCompare").click(function () {
$.ajax(
{
type: "POST",
data: { FileString1: $("#ComparisonOutput").html(),
FileString2: $("#EdgarDoc").html()
},
url: '/Home/GetCompareResult',
success: function (response) {
debugger;
$("#ComparisonOutput").html(response);
},
failure: function (response) {
},
error: function
(response) {
},
complete: function () {
}
});
});
</script>
<table>
<tr>
<td>
<div>
<label class="bold">
Content 1</label>
</div>
<div id="ComparisonOutput" style="height: 300px;
border-style:
solid; border-width: 1pt; vertical-align: middle;">
<p> <b>Content 1 </b></p>
</div>
</td>
<td>
<div>
<label class="bold">
Content 2</label>
</div>
<div id="EdgarDoc"
style="height: 300px; border-style: solid;
border-width:
1pt; vertical-align: middle;">
<p> <b>Content 2 </b></p>
</div>
</td>
</tr>
</table>
<br />
<input type="submit"
name="btnCompare"
id="btnCompare"
title="Compare"
value="Compare">
ControllerName: HomeController.cs
/// <summary>
/// [HttpPost] is attribute needed for all form submission
using Post method including submission from Ajax
/// [ValidateInput(false)] needed for disabling security
validation on input contents so that HTML data can be submitted using ajax
/// </summary>
/// <param
name="FileString1"> HTML String </param>
/// <param
name="FileString2"> HTML String </param>
/// <returns>Processed Output String</returns>
[HttpPost]
[ValidateInput(false)]
public string GetCompareResult(string
FileString1, string FileString2)
{
string
result = string.Empty;
//////Body
return
result;
}
No comments:
Post a Comment