0

我正在开发一个需要对控件进行内联编辑的项目。我想使用视图编辑模型的属性,然后想使用 jquery ajax 将其发送到控制器。如何使用 $.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'
            },

在这篇 ajax 发布如何改进它之后,我在方法 updateDetail 中得到了 null

4

3 回答 3

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 ){

            }
        });
    });
于 2012-11-19T07:46:52.377 回答
0

由于您的实体成员都是简单类型(int - string - decimal),您可以遍历表单元素并创建一个 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 发送它:

 $.ajax({
        url: '/Test/updateDetail',
        type: 'POST',
        data: dataToSend 
       });
于 2012-11-19T07:18:36.900 回答
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
                        }
                    });                    
                });
于 2012-11-19T07:46:19.510 回答