C# MVC4中如何将参数解析为局部视图方法

C# MVC4中如何将参数解析为局部视图方法,c#,ajax,asp.net-mvc,asp.net-mvc-4,json.net,C#,Ajax,Asp.net Mvc,Asp.net Mvc 4,Json.net,我在VS2012中使用MVC4 要求:希望通过部分查看和从方法加载数据来显示网格数据 问题:我成功地将数据从方法加载到局部视图,但没有参数 在index.cshtml中 <div id="gridContent"></div> <script src="~/Content/SB/js/json2.js"></script> <script> $(document).ready(function () { $(

我在VS2012中使用MVC4

要求:希望通过部分查看和从方法加载数据来显示网格数据

问题:我成功地将数据从方法加载到局部视图,但没有参数

在index.cshtml中

<div id="gridContent"></div>

<script src="~/Content/SB/js/json2.js"></script>

<script>
    $(document).ready(function () {
        $("#btnGo").click(function () {             
            loadGrid();
        });          
    });
</script>

 <script>
     function loadGrid() {
         var booksDiv = $("#gridContent");
         var items = {};
         items.vFinYear = $("#ddl_FinnYear").val();
         items.vDeptCode = $("#ddl_Dept").val();

         $.ajax({
             cache: false,
             type: "GET",
             url: "/Voucher/VoucherRaisedbyMePartial" ,
             data: '{items: ' + items + '}',
             success: function (data) {
                 booksDiv.html('');
                 booksDiv.html(data);
             },
             error: function (xhr, ajaxOptions, thrownError) {
                 debugger;
                 alert(thrownError);
                 alert('Failed to retrieve data.');
             }
         });
     }
</script>

使用HttpPost属性将操作更改为post

[HttpPost]
public ActionResult VoucherRaisedbyMePartial(Voucher items)
在ajax调用中,可以像下面这样直接向json对象传递

 $.ajax({
     cache: false,
     type: "POST",
     url: "/Voucher/VoucherRaisedbyMePartial" ,
     data: { items: items },
     success: function (data) {
         booksDiv.html('');
         booksDiv.html(data);
     },
     error: function (xhr, ajaxOptions, thrownError) {
         debugger;
         alert(thrownError);
         alert('Failed to retrieve data.');
     }
 });

更改为post,内容类型为application/json;字符集=utf-8

  $.ajax({
             cache: false,
             type: "POST",
             contentType: "application/json; charset=utf-8",
             url: "/Voucher/VoucherRaisedbyMePartial",
             data: '{items: ' + JSON.stringify(items) + '}',

             success: function (data) {
                 booksDiv.html('');
                 booksDiv.html(data);
             },
             error: function (xhr, ajaxOptions, thrownError) {
                 debugger;
                 alert(thrownError);
                 alert('Failed to retrieve data.');
             }
         });

显示您的
凭证的型号
(但是为什么不在方法中只使用两个参数
vFinYear
vDeptCode
?而且您不需要对数据进行字符串化-它只是
data:items
add
contentType:application/json;
数据类型:'HTML'
在ajax选项中。stephen……做了,但没有改变..通过在ajax调用中删除stringify,您正在使用数据:{items:'+items+'},我认为它可能会将您的数据转换为字符串类型。请尝试使用数据:{'items':items}将您的方法更改为
public ActionResult voucherraisedbymepial(字符串vFinYear,字符串vDeptCode)
并使用
data:items:
您不需要
contentType
选项。删除它,只需使用
data:items
data:{vFinYear=$(“\ddl\u FinnYear”).val(),vDeptCode=$(“\ddl\u Dept”).val()}
 $.ajax({
     cache: false,
     type: "POST",
     url: "/Voucher/VoucherRaisedbyMePartial" ,
     data: { items: items },
     success: function (data) {
         booksDiv.html('');
         booksDiv.html(data);
     },
     error: function (xhr, ajaxOptions, thrownError) {
         debugger;
         alert(thrownError);
         alert('Failed to retrieve data.');
     }
 });
  $.ajax({
             cache: false,
             type: "POST",
             contentType: "application/json; charset=utf-8",
             url: "/Voucher/VoucherRaisedbyMePartial",
             data: '{items: ' + JSON.stringify(items) + '}',

             success: function (data) {
                 booksDiv.html('');
                 booksDiv.html(data);
             },
             error: function (xhr, ajaxOptions, thrownError) {
                 debugger;
                 alert(thrownError);
                 alert('Failed to retrieve data.');
             }
         });