【问题标题】:ASP.Net MVC Get ID of newly added record from AJAX postASP.Net MVC 从 AJAX 帖子中获取新添加记录的 ID
【发布时间】:2013-03-29 14:53:56
【问题描述】:

在以下 JQuery/Ajax 发布到它之后,我如何从以下模型中检索 ID

JQuery:

       $.ajax({
            type: 'POST',
            url: '/api/searchapi/Post',
            contentType: 'application/json; charset=utf-8',
            data: toSend,
        }).done(function (msg) {
                alert( "Data Saved: " + msg );
        });

控制器:

    // POST api/searchapi
    public Void Post(Booking booking)
    {
        if (ModelState.IsValid)
        {
            tblCustomerBooking cust = new tblCustomerBooking();
            cust.customer_email = booking.Email;
            cust.customer_name = booking.Name;
            cust.customer_tel = booking.Tel;

            bc.tblCustomerBookings.Add(cust);
            bc.SaveChanges();

            long ID = cust.customer_id;

            Return ID;   <-- what do I enter here?
         }

         Return "Error";  <-- and here?
     }  

如何将 ID 取回 jQuery 脚本,如果模型无效,如何向 jQuery 返回错误?

感谢您的帮助,

标记

【问题讨论】:

  • 你的控制器编译了吗?您的 post 方法具有 void 返回类型,因此当它看到返回 ID 并返回“错误”命令时,它应该会编译失败
  • 您好-返回 ID 错误,“无法将类型 'long' 隐式转换为 'System.Web.Mvc.JsonResult'” - 与返回“错误”相同; - 谢谢你。标记

标签: asp.net asp.net-mvc asp.net-mvc-3 jquery


【解决方案1】:

你可以返回一个 JsonResult

[HttpPost]
public ActionResult Post(Booking booking)
{
    if (ModelState.IsValid)
    {
        tblCustomerBooking cust = new tblCustomerBooking();
        cust.customer_email = booking.Email;
        cust.customer_name = booking.Name;
        cust.customer_tel = booking.Tel;

        bc.tblCustomerBookings.Add(cust);
        bc.SaveChanges();

        return Json(new { id = cust.customer_id });
     }

     return HttpNotFound();
 }  

然后简单地在客户端上:

$.ajax({
    type: 'POST',
    url: '/api/searchapi/Post',
    contentType: 'application/json; charset=utf-8',
    data: toSend,
}).done(function (msg) {
    alert('Customer id: ' + msg.id);
}).error(function(){
    // do something if the request failed
});

【讨论】:

    【解决方案2】:

    一种方法是这样的:

    在您的控制器中:

     public Void Post(Booking booking)
        {
    
             //if valid
             return Json(new {Success = true, Id = 5}); //5 as an example
    
             // if there's an error
             return Json(new {Success = false, Message = "your error message"}); //5 as an example
         }  
    

    在您的 ajax 帖子中:

    $.ajax({
            type: 'POST',
            url: '/api/searchapi/Post',
            contentType: 'application/json; charset=utf-8',
            data: toSend,
            success: function(result) {
              if (result.Success) {
                alert(result.Id);
              }
              else {
                alert(result.Message);
              }
            }
    });
    

    【讨论】:

      【解决方案3】:

      返回 void 不是一个好主意,使用 JsonResult

      因为您使用的是 JavaScript,我建议使用 JSON。

      return this.Json(new { customerId = cust.customer_id});
      

      【讨论】:

        【解决方案4】:

        为了接收返回值,控制器方法必须返回 void 以外的东西。

        你的控制器能编译吗?您的 post 方法具有 void 返回类型,因此当它看到返回 ID 并返回“错误”命令时,它应该编译失败

        【讨论】:

          猜你喜欢
          • 2022-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多