簡體   English   中英

mvc4 jquery .ajax成功未觸發

[英]mvc4 jquery .ajax success not fired

我有一個局部視圖。 我在視圖中有一個模型,在局部視圖中有一個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)

        }

這就是我使用郵件表格的方式!

查看我使用JsonResult而不是ActionResult的控制器中的簽名! 我也返回JSON不是視圖!

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

    });
};

您應該決定是否要發送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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM