2012-11-19 31 views
0

我正在一個項目,我需要內聯編輯控件。我想使用視圖編輯模型的屬性,然後想用jquery ajax將它發送給控制器。我怎麼能發送完整的模型中使用$阿賈克斯()

我的模型是控制器:

public class LeadDetail 
    { 
    public int EntityID { get; set; } 
    public string EntityName { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Email { get; set; } 
    public int PhoneType { get; set; } 
    public string PhoneNumber { get; set; } 
    public string PhoneExt { get; set; } 
    public string Address1 { get; set; } 
    public string Address2 { get; set; } 
    public string Address3 { get; set; } 
    public string City { get; set; } 
    public int State { get; set; } 
    public string ZipCode { get; set; } 
    public string Country { get; set; } 
    public decimal Amount { get; set; } 
    public string LeadAssignedToName { get; set; } 
    public int RelatedAccount { get; set; } 
    public string LeadStatusName { get; set; } 
    public string LeadRatingName { get; set; } 
    public string LeadSourceName { get; set; } 
    public string CampaignName { get; set; } 
    public int LeadType { get; set; } 
    public string Url { get; set; } 
    public string Comments { get; set; } 
    public int Subscribed { get; set; } 
    public string CreatedDate { get; set; } 
    public string CreatedBy { get; set; } 
    public string utm_source { get; set; } 
    public string utm_term { get; set; } 
    public string IPAddress { get; set; } 
    public string utm_medium { get; set; } 
    public string utm_campaign { get; set; } 
    public string utm_content { get; set; } 
    public int RelatedAccountId { get; set; } 
    public int LeadTypeID { get; set; } 
    public int CampaignID { get; set; } 
    public string BirthDate { get; set; } 
    public string SocialSecurity { get; set; } 
    public string emailsubscriber { get; set; } 
    public string active { get; set; } 
    public int department { get; set; } 
    public int Title { get; set; } 
    public int PrimaryEmail { get; set; } 
    public int PrimaryPhone { get; set; } 
    public int PrimaryAddress { get; set; } 
    public string LeadAssignedDate { get; set; } 
    public int LeadRatingID { get; set; } 
    public int LeadStatusID { get; set; } 
    public int AccountType { get; set; } 
    public int EntityTypeID { get; set; } 
    public int LeadAssignedTo { get; set; } 
    public int AccountStatusID { get; set; } 
    public int LeadSourceID { get; set; } 
    public string PrimaryContact { get; set; } 
    public string stateName { get; set; } 
} 

和我對控制器的方法:

[HttpPost] 
    public void updateDetail(LeadDetail Lead) 
    { 
     LeadDetail leadvalue = Lead; 
    } 

我想正確的方式發送全模型到控制器

$.ajax({ 
      url: '/Test/updateDetail', 
      type: 'POST', 
      data: { 
       Lead:'@Model' 
      }, 

我在方法updateDetail後得到null ajax後如何提高它

回答

1
Lead:'@Model' 

這是服務器代碼,它只能在* .cshtml文件,如果你看看在呈現的html標記中,您只會找到對象名稱。

試試這個:

var form = $('#your_form'); 
    form.submit(function (e) { 
     e.preventDefault(); 
     $.ajax({ 
      url: this.action, 
      type: this.method, 
      data: $(this).serialize(), 
      success: function (data){ 

      } 
     }); 
    }); 
+0

嗨... u能請告訴我如何發送序列化類對象和int值使用Ajax控制器? – user3217843

0

作爲實體成員都是簡單類型(int - 字符串 - 十進制),您可以遍歷表單元素並創建json對象。

var rawData = "{"; 
    var fieldCount = $("input , textarea , select ",$(".yourform")).length; 
    $("input , textarea , select ", $(".yourform")).each(function (index) { 
     if ($(this).attr("value") == undefined || $(this).attr("name") == undefined) {    
      return; 
     } 
     rawData += '"' + $(this).attr("name") + '" : "' + $(this).attr("value") + '"'; 
     if (index < fieldCount - 1) 
      rawData += ","; 
    }); 

    rawData += "}"; 

    var dataToSend = $.parseJSON(rawData); 

那麼你就可以找你$就發送:

$.ajax({ 
     url: '/Test/updateDetail', 
     type: 'POST', 
     data: dataToSend 
     }); 
0
   $('#form').submit(function() { 
        $.ajax({ 
         url: url 
         type: 'POST', 
         data: $(this).serialize(), // 'this' is your form or use form id 
         success: function (result) { 
         //your action after submit 
         } 
        });      
       }); 
相關問題