AJAX传递数据始终为null

时间:2013-04-12 00:25:11

标签: c# ajax asp.net-mvc

我想传递模型数据,认为Ajax函数这是我的观点。这是局部视图

@model LMS.Models.COM_DELIVERY

            <script type="text/javascript">  
                function saveDelivery() {                  
                    $.ajax({

                        type: 'POST',
                        dataType: 'JSON',
                        url: '@Url.Action("SaveDelivery", "Business")',
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify($('#addDelivaryform').serialize()),

                        success: function (result) {

                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert(xhr.status);
                            alert(thrownError);
                        }
                    });

                }
    </script>

    @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "addDelivaryform" }))
{ 


    @Html.ValidationSummary(true)

        <legend>Create Delivery </legend>
        <table>

        <tr><td>
        Contact
        </td>
        <td>
           @Html.EditorFor(model => model.CONTACT)
            @Html.ValidationMessageFor(model => model.CONTACT)

        </td></tr>

         <tr><td> Phone      
        </td>
        <td>       
        @Html.EditorFor(model => model.PHONE)
            @Html.ValidationMessageFor(model => model.PHONE)
        </td></tr>

           <tr><td>  Address    
        </td>
        <td>       
         @Html.EditorFor(model => model.ADDRESS_1)
            @Html.ValidationMessageFor(model => model.ADDRESS_1)
        </td></tr>

           <tr><td>  Street    
        </td>
        <td>       
         @Html.EditorFor(model => model.STREET)
            @Html.ValidationMessageFor(model => model.STREET)
        </td></tr>

           <tr><td>   Suburb   
        </td>
        <td>       
          @Html.EditorFor(model => model.SUBURB)
            @Html.ValidationMessageFor(model => model.SUBURB)
        </td></tr>



           <tr><td colspan="2">    

            <input type="button" value="Save Order Type"   onclick="saveDelivery();"  />

        </td>
        </tr>
        </table>




}

这是控制器

public ActionResult SaveDelivery(COM_DELIVERY obj)
          {
             strig name = obj.CONTACT 
              return Json(new { success = true });
          }

在我的控制器obj.CONTACT中,即使我填写CONTACT也始终为null。 这是Ajax Post

JSON


There are no child objects

Source
"CONTACT=hhhh&PHONE=&ADDRESS_1=&STREET=&SUBURB=&"

所以如何获取控制器中的值。

1 个答案:

答案 0 :(得分:0)

尝试创建像

这样的匿名JavaScript对象
<script type="text/javascript">  
                function saveDelivery() {     

                     var SendData = {
                               CONTACT: $.trim($('#Selecteditemid').val()),
                               PHONE: $.trim($('#Selecteditemid').val()),
                               ADDRESS_1: $.trim($('#Selecteditemid').val()),
                               STREET: $.trim($('#SelectedCompanyCode').val()),
                               SUBURB: $.trim($('#SelectedCompanyCode').val())
                              };
                    $.ajax({
                        type: 'POST',
                        dataType: 'JSON',
                        url: '@Url.Action("SaveDelivery", "Business")',
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify(SendData),
                        async: true,
                        processData: false,
                        success: function (result) {

                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert(xhr.status);
                            alert(thrownError);
                        }
                    });

                }
    </script>
/////////
[HttpPost]  
public ActionResult SaveDelivery(COM_DELIVERY obj)
 {
             strig name = obj.CONTACT 
             return Json(new { success = true });
 }