skip to Main Content

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


  1. because true don’t return as bool it returned as string
    you’r code should be like

            if (data=="True") {
                alert('asd');
                $('#butn').trigger('click');
            }
    
    Login or Signup to reply.
  2. Can you please try this

    1) Create one model class for generic using

     public class AjaxActionResult
     {
            public string Message { get; set; }
            public bool Success { get; set; }
            public object Data { get; set; }
     }
    
    //In your action 
    
    
    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(new AjaxActionResult()
            {
                Message = "Your message",
                Success = true
            });
        }
        catch(Exception ex)
        {
            return Json(new AjaxActionResult()
            {
                Message = ex.Message,
                Success = false,
                Data = ex
            });
        }
    }
    

    In jquery, you need to change as below

    $("#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.Success) {
                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);
        }
    });
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search