如何在MVC4中解析参数到局部视图方法

时间:2016-04-04 06:28:33

标签: c# ajax asp.net-mvc asp.net-mvc-4 json.net

我正在使用MVC4和VS2012

要求: 想要通过局部视图显示网格数据&从方法加载数据。

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

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>

in凭证控制器

    public ActionResult VoucherRaisedbyMePartial(Voucher items)
    {
        // Problem is here: Unable to get values in parameter items
        // Data loading in partial view sucessfully, but i want to filter
        // data on the basis of departmet and financial year.

        var VoucherList = new List<Voucher>();          
        return PartialView("_VoucherRaisedbyMePartial", VoucherList);
    }

优惠券型号

namespace PVS_WEB.Models
{
    public class Voucher
    {
        public string vFinYear { get; set; }
        [DisplayName("Department")]
        public string vDeptCode { get; set; }
        public string vVoucheNo { get; set; }
        public string vPaymentFor { get; set; }
        public string vStatus { get; set; }
        public string vPendingWith { get; set; }
        public string vVoucherSrNo { get; set; }
        public string vUserID { get; set; }

    [DisplayName("Financial Year")]
    public SelectList lstFinYear { get; set; }

    [DisplayName("Department")]
    public SelectList lstDepartment { get; set; }

    [DisplayName("From")]
    public string vFrom { get; set; }

    [DisplayName("To")]
    public string vTo { get; set; }

    [DisplayName("Date")]
    [DisplayFormat(DataFormatString = "{0:d MMM yyyy}")]
    public DateTime dtDate { get; set; }

    [DisplayName("Invoice Received Date")]
    [DisplayFormat(DataFormatString = "{0:d MMM yyyy}")]
    public DateTime dtInvoiceRecDate { get; set; }

    [DisplayName("Vendor")]
    public string vVendor { get; set; }

    [DisplayName("Cost Center")]
    public string vCostcenter { get; set; }

    [DisplayName("GLCode")]
    public string vGLCode { get; set; }

    [DisplayName("Debit To")]
    public string vDebitTo { get; set; }

    [DisplayName("Issue Cheque in favour of")]
    public string vIssuechequeinfav { get; set; }

    [DisplayName("Towards Payment For")]
    public string vTowardsPayment { get; set; }

    [DisplayName("Due Date")]
    [DisplayFormat(DataFormatString = "{0:d MMM yyyy}")]
    public DateTime dtDuecDate { get; set; }

    [DisplayName("Service Applicable")]
    public bool vServiceApplicable { get; set; }

    [DisplayName("Type of Service")]
    public string vServiceType { get; set; }

    [DisplayName("Service received From")]
    public string vServiceReceivedFrom { get; set; }

    [DisplayName("Applicability of service tax liability")]
    public string vServiceTaxLiability { get; set; }


    [DisplayName("Coupon ID")]
    public int nVoucherCouponID { get; set; }

    [DisplayName("Invoice Number")]
    public string vInvoiceNo { get; set; }

    [DisplayName("Invoice Date")]
    [DisplayFormat(DataFormatString = "{0:d MMM yyyy}")]
    public DateTime dtInvoiceDate { get; set; }

    [DisplayName("PO Number")]
    public string vPONo { get; set; }

    [DisplayName("PO Date")]
    [DisplayFormat(DataFormatString = "{0:d MMM yyyy}")]
    public DateTime dtPODate { get; set; }

    [DisplayName("SAP GIR No")]
    public string vSAPGIRNO { get; set; }

    [DisplayName("Invoice Amount")]
    public double dInvoiceAmt { get; set; }

    [DisplayName("Advance Date")]
    [DisplayFormat(DataFormatString = "{0:d MMM yyyy}")]
    public DateTime dtAdvanceDate { get; set; }

    [DisplayName("Advance Amount")]
    public double dAdvanceAmt { get; set; }

}
}

2 个答案:

答案 0 :(得分:0)

使用HttpPost属性

将操作更改为发布
[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.');
     }
 });

答案 1 :(得分:0)

更改是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.');
             }
         });