0

I am posting my data using Ajax, the data is successfully posted to the database. But I am not getting response back from the Create Method. I mean return RedirectToAction("Index"); the page is not redirecting to Index page.

// POST: TestModels/Create        
[HttpPost]
public ActionResult Create(TestModel testModel)
{
    if (ModelState.IsValid)
    {
        db.TestModels.Add(testModel);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(testModel);
}

Also, I tried:

// POST: TestModels/Create        
[HttpPost]
public JsonResult Create(TestModel testModel)
{
    if (ModelState.IsValid)
    {
        db.TestModels.Add(testModel);
        db.SaveChanges();
       return Json(new { success = true, responseText = "Your message successfuly sent!" }, JsonRequestBehavior.AllowGet);     
    }
    return Json(new { success = false, responseText = "Error." }, JsonRequestBehavior.AllowGet);
}

Still I am not getting any Json response.

My javascript function::

<script>
    $(document).ready(function () {
        $('#btn_submit').click(function () {

            var testModel = {
                FirstName: $('#FirstName').val(),
                LastName: $('#LastName').val(),
                Gender: $("input[name='Gender']:checked").val(),
                Contact: $('#Contact').val(),
                Faculty: $('#Faculty').val(),
                RegNepaliDate: $('#RegNepaliDate').val()
            };
            alert(JSON.stringify(testModel));

            $.ajax({
                type: "POST",
                url: "/TestModels/Create", 
                data: JSON.stringify(testModel),
                contentType: "application/json;charset=utf-8",
                dataType: 'json',
                success: function (response) {
                    console.log(response);
                    if (response.success) {
                        console.log(response.responseText);
                        console.log(data);
                    }
                },
                error: function (response) {
                    alert("failed...");
                  }  
            })

        })
    })
</script>

The data is posted to the server and when I browse Index page the newly inserted data is also shown. Also, when I click the submit button of the Create page, the form data becomes clear but all the data is shown in the url bar

https://localhost:44399/TestModels/Create?FirstName=machhi&LastName=go&Gender=male&Contact=5589512369&Faculty=science&RegNepaliDate=2021-05-01

user4221591
  • 2,084
  • 7
  • 34
  • 68
  • In Chrome, press F12 and switch to network tab and monitor the post request? Does it return a 200 status code? – Greg Oct 24 '21 at 10:47
  • Can you show you view too, pls? – Serge Oct 24 '21 at 12:08
  • @Greg While debugging using firefox developer tool, in Network -> POST method showing NS_BINDING_ABORTED in `Transferred` tab. – user4221591 Oct 27 '21 at 09:10
  • NS_BINDING_ABORTED might be a Firefox specific issue. Take a look at https://stackoverflow.com/questions/704561/ns-binding-aborted-shown-in-firefox-with-httpfox. I would recommend debugging your code with Chrome, to find out the response of the POST request – Greg Oct 27 '21 at 17:33

1 Answers1

0

Could you please add missing jQuery library file like "

<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/jquery-3.4.1.slim.min.js"></script>

" to get the response from Controller.

Mano
  • 780
  • 3
  • 15