简体   繁体   English

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

[英]how to parse parameter to to partial view method in MVC4

I am using MVC4 with VS2012 我在VS2012中使用MVC4

Requirement: Want to display Grid data by partial view & loading data from method. 要求:要通过部分视图显示网格数据并从方法中加载数据。

Problem: i sucessfully load data from method to partial view but without parameter. 问题:我成功地将数据从方法加载到局部视图,但没有参数。

in index.cshtml 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 voucher Controller 在凭证控制器中

    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);
    }

Voucher Model 优惠券模型

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; }

}
}

Change you action to post by using HttpPost attribute 使用HttpPost属性将您的操作更改为发布

[HttpPost]
public ActionResult VoucherRaisedbyMePartial(Voucher items)

In you ajax call you can pass directly the the json object like this 在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.');
     }
 });

changes are post and content type is application/json; 更改为post,内容类型为application / json; charset=utf-8 字符集= 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.');
             }
         });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM