Reputation: 43
this is my ajax code for an onclick of a button. The alert box shows me the values are correct. But when it attempts to go to the controller it gives me a 500 error. The controller is ContentPackController.
$(function () {
$('#updateContent').click(function () {
var selectedValue = $('#contentAlias').val();
var value = $('#contentPackDetails').val();
alert(selectedValue + " " + value);
$.ajax({
url: '@Url.Action("UpdateContent", "ContentPack")',
type: "POST",
data: { PropId: selectedValue, UpdateText: 'Looking For An <span class="embellish">Great Deal</span> On Your Next Home?"' },
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}, success: function () {
alert("Updates have been saved");
}
});
});
});
This is the ActionResult that should be called
public ActionResult UpdateContent(int PropId, string UpdateText)
{
using (var entities = new OpenRoadEntities())
{
var prop = entities.ContentPackDetails.FirstOrDefault(c => c.Id==PropId);
prop.Value = UpdateText;
entities.SaveChanges();
return Json(new EmptyResult(),JsonRequestBehavior.AllowGet);
}
}
This is just driving me crazy. Other calls to the same controller work correctly.
Upvotes: 0
Views: 54
Reputation: 7211
It's due to the HTML tags in your POST data. MVC validates the data and throws an error if it detects potentially unsafe content. Either come up with some sort of markup that gets converted in the action or add the [ValidateInput(false)]
attribute to your action.
Upvotes: 1