简体   繁体   English

控制器方法在成功执行后不返回任何响应

[英]controller method not returning any response after successful execution

I am posting my data using Ajax, the data is successfully posted to the database.我正在使用 Ajax 发布我的数据,数据已成功发布到数据库。 But I am not getting response back from the Create Method.但是我没有从Create Method 得到响应。 I mean return RedirectToAction("Index");我的意思是 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.我仍然没有收到任何Json响应。

My javascript function::我的 javascript 函数::

<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.数据被发布到服务器,当我浏览Index页面时,也会显示新插入的数据。 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此外,当我单击Create页面的提交按钮时,表单数据变得清晰,但所有数据都显示在 url 栏中

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

Could you please add missing jQuery library file like "您能否添加缺少的 jQuery 库文件,例如“

<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. " 以获取 Controller 的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM