My jQuery code looks like:
$("#create").click(function(e) {
var myModel =
{
"TribeName": $('#TribeName').val()
};
var jsonToPost = JSON.stringify(myModel);
$.ajax({
url: '/Home/Create/',
async: true,
processData: false,
data: jsonToPost,
type: 'post',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data) {
alert('asd');
$('#butn').trigger('click');
}
},
error: function (err) {
if (!$('.modal-header').hasClass('alert-danger')) {
$('.modal-header').removeClass('alert-success').addClass('alert-danger');
$('.delete-confirm').css('display', 'none');
}
$('.success-message').html(err.statusText);
}
});
})
Here is my controller:
public ActionResult Create(Tribe model)
{
try
{
using (StructureEntities db = new StructureEntities())
{
var tribe = db.Set<Tribe>();
tribe.Add(new Tribe {TribeName = model.TribeName });
//db.Tribes.Add(model);
db.SaveChanges();
}
return Json(true, JsonRequestBehavior.AllowGet);
}
catch(Exception ex)
{
return Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
}
// return RedirectToAction("NewsCreated", model);
}
My making it type get I am getting back to success method but due to type I can’t send my data to controller.
In this above ajax I can send and save my data but cant get back to success method of ajax
I searched a lot before posting this question
Hopes for your suggestions
2
Answers
because true don’t return as bool it returned as string
you’r code should be like
Can you please try this
1) Create one model class for generic using
In jquery, you need to change as below