0

我想通过 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 始终为 null,即使我填写了 CONTACT 。这是阿贾克斯邮报

JSON


There are no child objects

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

那么如何获取控制器中的值。

4

1 回答 1

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 });
 }
于 2013-04-12T01:03:46.807 回答