mvc4 jquery .ajax成功没被解雇

时间:2014-04-17 20:13:22

标签: javascript jquery ajax asp.net-mvc asp.net-mvc-4

我有一个局部视图的视图。我在视图中有一个模型,在局部视图中有一个kendogrid。我也有一个ajax调用控制器只是更新我的模型而不返回视图。假设我需要单击生成id的网格上的工具栏按钮。现在我想将该id返回给视图(通过使用该id更新模型)。但成功(数据)没有解雇

$.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
            },
        });

控制器

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)

        }

2 个答案:

答案 0 :(得分:0)

这是我使用邮件表格的方式!

查看控制器中的签名我使用 JsonResult 而不是 ActionResult ! 我还返回JSON而不是View!

return Json(result);

在我的控制器中

 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

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

    });
};

答案 1 :(得分:0)

您应该决定是要发送HTML结果(视图)还是JSON结果(Json),并在Action中调用相应的结果。如果在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
}