简体   繁体   English

mvc4 jquery .ajax成功未触发

[英]mvc4 jquery .ajax success not fired

I have a view with a partial view in it. 我有一个局部视图。 I have a model in the view and a kendogrid in the partial view. 我在视图中有一个模型,在局部视图中有一个kendogrid。 I also have an ajax call to teh controller just updates my model without returning a view. 我也有一个ajax调用控制器,只是更新了我的模型而没有返回视图。 Say I need to click a toolbar button on the grid that generates an id. 说我需要单击生成ID的网格上的工具栏按钮。 Now i want to return that id to the view(by updating model with that id). 现在,我想将该ID返回到视图(通过使用该ID更新模型)。 But success(data) is not firing 但是成功(数据)并没有激发

$.ajax({
            type: "POST",
            data: JSON.stringify({ Id: pId, schId: sId}),
            contentType: "application/json; charset=utf-8",
            dataType: "JSON",
            cache: false,
            url: '@(Url.Action("Process", "Controller"))',
            success: function (data) {
                var abc = data.InvoiceId;---->not fired
            },
        });

Controller 控制者

public ActionResult Process(int Id, int schId, SearchModel mymodel, [DataSourceRequest]DataSourceRequest request)
        {
            int myId = -1;

                // generate the Id);
        myId = generatenewId(Id,schId);-- this gets generated and myId is updated
            }

            mymodel.Id = myId 
            return View(mymodel)

        }

This is how I do it using a mail form! 这就是我使用邮件表格的方式!

Look at the signature in my controller I use JsonResult not ActionResult ! 查看我使用JsonResult而不是ActionResult的控制器中的签名! Also I return JSON not a View! 我也返回JSON不是视图!

return Json(result);

IN MY CONTROLLER 在我的控制器中

 public JsonResult AjaxMailer(MailerModel model)
 {
            Emailer mailer = new Emailer();
            JsonResult Jr = new JsonResult();
            string result = mailer.DispatchEmail(model.phone, model.name, model.message, model.email);
            return Json(result);
 }

JAVASCRIPT IN MY VIEW 我认为JAVASCRIPT

function imClicked(e) {
    e.preventDefault();
    var messageObj = 
   {
       "name": "",
       "email": "",
       "phone": "",
       "message": "",

   };
    messageObj.name = $("#name").val();
    messageObj.email = $("#email").val();
    messageObj.phone = $("#phone").val();
    messageObj.message = $("#message").val();

    $.ajax({
        dataType: "json",
        contentType: 'application/json',
        type: 'POST',
        url: '/Contact/AjaxMailer',
        data: JSON.stringify(messageObj),
        error: printError,
        success: mailsent

    });
};

You should decide wether you want to send a HTML result (view), or JSON result (Json), and call the corresponding result in your Action. 您应该决定是否要发送HTML结果(视图)或JSON结果(Json),然后在Action中调用相应的结果。 If you set the dataType to JSON in your jQuery Ajax call, you should return Json result, for example: 如果在jQuery Ajax调用中将dataType设置为JSON,则应返回Json结果,例如:

public ActionResult Process(int Id, int schId, SearchModel mymodel, [DataSourceRequest]DataSourceRequest request)
{
  int myId = -1;
  // generate the Id);
  myId = generatenewId(Id,schId);-- this gets generated and myId is updated
  mymodel.Id = myId 
  return Json(mymodel) // this will return Json in the response containing your model object
  //return View(mymodel) !!! This would return a full HTML response rendered with you model
}

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

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