2013-07-22 36 views
10

Tôi đang cố gắng thay thế gửi biểu mẫu bằng cuộc gọi ajax. hành động cần formcollection và tôi không muốn tạo một Model mới. Vì vậy, tôi cần phải vượt qua toàn bộ hình thức (giống như gửi mẫu) nhưng thông qua cuộc gọi ajax. Tôi đã cố gắng tuần tự hóa và sử dụng Json nhưng tính năng formcollection trống. đây là chữ ký hành động của tôi:Làm thế nào để vượt qua formcollection bằng cách sử dụng ajax gọi đến một hành động?

public ActionResult CompleteRegisteration(FormCollection formCollection) 

và đây là nút của tôi nộp nhấp chuột:

var form = $("#onlineform").serialize();    
      $.ajax({ 
       url: "/Register/CompleteRegisteration",     
       datatype: 'json', 
       data: JSON.stringify(form), 
       contentType: "application/json; charset=utf-8",     
       success: function (data) { 
        if (data.result == "Error") { 
         alert(data.message); 
        } 
       } 
      }); 

nay làm thế nào tôi có thể truyền dữ liệu vào FormCollection?

Trả lời

29

Kể từ FormCollection là một số cặp khóa-giá trị, JSON là định dạng dữ liệu không phù hợp cho đại diện của mình. Bạn nên sử dụng chỉ serialized chuỗi có dạng:

var form = $("#onlineform").serialize(); 
$.ajax({ 
    type: 'POST', 
    url: "/Register/CompleteRegisteration", 
    data: form, 
    dataType: 'json', 
    success: function (data) { 
     if (data.result == "Error") { 
      alert(data.message); 
     } 
    } 
}); 

thay đổi chính:

  1. loại được yêu cầu thiết lập để POST (không cần thiết ở đây, nhưng có vẻ tự nhiên hơn)
  2. dạng In nhiều thay vì Chuỗi JSON theo yêu cầu dữ liệu
  3. contentType bị xóa - chúng tôi không gửi JSON nữa
+0

Nên có "," sau khi nhập: 'POST' dòng – PAVITRA

+1

@PAVITRA, được thêm vào, cảm ơn – Andrei

5

Hãy thử:

$(<your form>).on('submit',function(){ 
    $.ajax({ 
     url: "/Register/CompleteRegisteration" + $(this).serialize(), 
     // place the serialized inputs in the ajax call     
     datatype: 'json', 
     contentType: "application/json; charset=utf-8",     
     success: function (data) { 
      if (data.result == "Error") { 
       alert(data.message); 
      } 
     } 
    }); 
}); 
+0

Cảm ơn bạn. Đính kèm cuộc gọi ajax để gửi là chìa khóa! – Armen

1

Nếu có ai muốn thêm dữ liệu bổ sung vào FormCollection thì bạn có thể thử bên dưới.

<script type="text/javascript"> 
function SubmitInfo() 
{ 
    var id = $("#txtid").val();  
    var formData = $('#yourformname').serializeObject(); 
    $.extend(formData, { 'User': id }); //Send Additional data 

    $.ajax({ 
     url: 'Controlle/GetUser', 
     cache: false, 
     type: 'POST', 
     dataType: 'json', 
     data: decodeURIComponent($.param(formData)), 
     success: function (data) { 
      $('#resultarea').html(data); 
     }, 
     error: function (jqXHR, textStatus, errorThrown) { 
      alert("AJAX error: " + textStatus + ' : ' + errorThrown); 
     } 
    }); 
} 

$.fn.serializeObject = function() { 
    var o = {}; 
    var a = this.serializeArray(); 
    $.each(a, function() { 
     if (o[this.name] !== undefined) { 
      if (!o[this.name].push) { 
       o[this.name] = [o[this.name]]; 
      } 
      o[this.name].push(this.value || ''); 
     } else { 
      o[this.name] = this.value || ''; 
     } 
    }); 
    return o; 
}; 
<script/> 

Action Method

public ActionResult GetUser(FormCollection frm) 
    { 
    int UserId = Convert.ToInt32(frm["user"]); 
    // your code 
    return Json(data, JsonRequestBehavior.AllowGet); 
    } 

Tham khảo link để biết thêm chi tiết. http://geekswithblogs.net/rgupta/archive/2014/06/25/combining-jquery-form-serialize-and-json.stringify-when-doing-ajax-post.aspx

+0

Theo đề xuất của người dùng ngăn xếp, tôi đã chỉnh sửa mã đăng & thêm & mô tả. –

Các vấn đề liên quan