如何在用户单击Jquery对话框中的按钮后运行控制器操作,代码必须遍历所有记录,以便用户可以接受或拒绝

时间:2016-01-25 14:34:10

标签: c# jquery asp.net-mvc

我正在与MVC中的一些代码作斗争,我设法将数据表单控制器传递给Jquery对话框小部件,现在我需要知道如何返回控制器,具体取决于用户是接受还是拒绝了记录。有一个我作为CSV上传到MVC View的记录列表,然后我有一个名为Validate Claims的按钮调用存储过程来验证记录,当点击验证声明时弹出一个对话框,控制器ViewBag的响应通过,我想基于验证响应启用用户接受或拒绝记录,当用户接受记录时,它必须将其保存到db然后移动到下一条记录。我如何在MVC JQuery中执行此操作,请协助。

请参阅下面的代码:

这是我的观看代码

    @{
    ViewBag.Title = "Home Page";
}

@*<link href="~/Content/jquery-ui.css" rel="stylesheet" />*@
<script src="~/Scripts/jquery-ui-1.11.4.min.js"></script>
<script src="~/Scripts/jquery-ui-1.11.4.js"></script>
<link href="~/Content/themes/base/dialog.css" rel="stylesheet" />
@*<script src="~/Scripts/jquery-1.8.2.js"></script>
    <script src="~/Scripts/jquery-ui.js"></script>*@

@using CSVSupplierClaims.Models
@model List<CSVSupplierClaims.Models.SupplierClaimsUploadDisplayList>



<input type="submit" id="validateClaims" value="Validate Claims" size="5" />
<input type="submit" value="Import Claims to CRM" size="5" />

<div id="dialog" title="Supplier Claims Validation">
    Claims Upload Confirmation
    <p>
        <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
        <table id="users" class="ui-widget ui-widget-content" height="100" width="100" border="1">
            <thead>
                <tr class="ui-widget-header ">
                    <th style="width:70%">ST Key</th>
                    <th style="width:70%">Supplier Claim</th>
                    <th style="width:70%">System Cost</th>
                    <th style="width:70%">Orig Inv</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>@ViewBag.ST_Key</td>
                    <td>@ViewBag.SupplierClaim</td>
                    <td>@ViewBag.OrigInv</td>
                    <td>@ViewBag.Error</td>
                    <td>@ViewBag.SystemCost</td>
                </tr>
            </tbody>
        </table>
    </p>
</div>



<script type="text/javascript">
    $(function () {
        $("#dialog").dialog({
            autoOpen: false
        });

        $("#validateClaims").click(function () {
            $("#dialog").dialog("open", "resizable");
            $("#dialog").dialog({
                resizable: true,
                height: 300,
                width:500,
                modal: true,
                closeOnEscape: true,
                buttons: {

                    "Accept Claim Record": function (){ 
                        $(this).dialog("close");
                        $(this).empty();
                    },
                    "Reject Claim Record": function () {
                        $(this).dialog("close");
                    }
                }
            });
        });
    });

</script>



<table>
    <tr>
        <th>Action</th>
        <th>LineNo</th>
        <th>TotalClaim</th>
        <th>ClaimReference</th>
        <th>Currency</th>
    </tr>
    @if (Model != null)
    {
        foreach (var c in Model)
        {
            <tr>
                <td>@c.Action</td>
                <td>@c.LineNo</td>
                <td>@c.TotalClaim</td>
                <td>@c.ClaimReference</td>
                <td>@c.Currency</td>
            </tr>
        }
    }

</table>
@Html.ValidationMessage("Error")

<form action="" method="post" enctype="multipart/form-data">

    <table style="margin-top:150px">
        <tr>
            <td>
                <label for="file"> Filename</label>
            </td>
            <td>
                <input type="file" name="file" id="file" />
            </td>
            <td>
                <input type="submit" value="Upload" />
            </td>
        </tr>
    </table>

这是我的控制器代码

using CsvHelper;
using CSVSupplierClaims.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Crm;
using System.Data.SqlClient;
using System.Data;

namespace CSVSupplierClaims.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index(HttpPostedFileBase file)
        {
            string path = null;

            List<SupplierClaimsUploadDisplayList> supplierClaimsData = new List<SupplierClaimsUploadDisplayList>();

            try
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    path = AppDomain.CurrentDomain.BaseDirectory + "upload\\" + fileName;
                    file.SaveAs(path);

                    var csv = new CsvReader(new StreamReader(path));

                    var supplierList = csv.GetRecords<SupplierClaimsUpload>();



                    foreach (var supplier in supplierList)
                    {
                        SupplierClaimsUploadDisplayList supplierUploadDisplay = new SupplierClaimsUploadDisplayList();

                        supplierUploadDisplay.Action = supplier.Action;
                        supplierUploadDisplay.LineNo = supplier.LineNo;
                        supplierUploadDisplay.TotalClaim = supplier.TotalClaim;
                        supplierUploadDisplay.ClaimReference = supplier.ClaimReference;
                        supplierUploadDisplay.Currency = supplier.Currency;

                        supplierClaimsData.Add(supplierUploadDisplay);
                    }

                }
            }
            catch
            {
                ViewData["error"] = "Uplaod failed";
            }
            Supplier_Claim_Upload_Result supplierClaimUplaod = new Supplier_Claim_Upload_Result();

            var sqlConnection = "data source=WMVSQL02;initial catalog=Embrace;integrated security=True;";

            using (SqlConnection conn = new SqlConnection(sqlConnection))
            {
                try
                {
                    foreach (var claim in supplierClaimsData)
                    {
                        SqlCommand cmd = new SqlCommand();
                        cmd.CommandTimeout = 60;
                        SqlDataReader reader;
                        cmd.CommandText = "CRM.Supplier_Claim_Upload";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@Invoice", SqlDbType.NVarChar).Value = claim.LineNo;
                        cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = claim.TotalClaim;
                        cmd.Connection = conn;

                        conn.Open();
                        reader = cmd.ExecuteReader();

                        while (reader.Read())
                        {
                            supplierClaimUplaod.ST_Key = reader["ST_Key"].ToString();
                            if (supplierClaimUplaod.SupplierClaim != null)
                            {
                                supplierClaimUplaod.SupplierClaim = reader["Supplier_Claim"].ToString();
                            }
                            else if (supplierClaimUplaod.SupplierClaim == null && supplierClaimUplaod.OrigInv == null && supplierClaimUplaod.SystemCost == null)
                            {
                                if (supplierClaimUplaod.Error != null)
                                {
                                    supplierClaimUplaod.Error = reader["Error"].ToString();
                                }
                                else if (supplierClaimUplaod.Error == null)
                                {
                                    supplierClaimUplaod.SupplierClaim = "No value";
                                }
                            }
                            if (supplierClaimUplaod.OrigInv != null)
                            {
                                supplierClaimUplaod.OrigInv = reader["Orig_Inv"].ToString();
                            }
                            else if (supplierClaimUplaod.OrigInv == null)
                            {
                                if (supplierClaimUplaod.Error != null)
                                {
                                    supplierClaimUplaod.Error = reader["Error"].ToString();
                                }
                                else if (supplierClaimUplaod.Error == null)
                                {
                                    supplierClaimUplaod.OrigInv = "No value";
                                }
                            }
                            if (supplierClaimUplaod.SystemCost != null)
                            {
                                supplierClaimUplaod.SystemCost = reader["System_Cost"].ToString();
                            }
                            else if (supplierClaimUplaod.SystemCost == null)
                            {
                                if (supplierClaimUplaod.Error != null)
                                {
                                    supplierClaimUplaod.Error = reader["Error"].ToString();
                                }
                                else if (supplierClaimUplaod.Error == null)
                                {
                                    supplierClaimUplaod.SystemCost = "No Value";
                                }
                            }
                        }


                        if (supplierClaimUplaod != null)
                        {
                            ViewBag.ST_Key = supplierClaimUplaod.ST_Key;
                            ViewBag.SupplierClaim = supplierClaimUplaod.SupplierClaim;
                            ViewBag.OrigInv = supplierClaimUplaod.OrigInv;
                            ViewBag.Error = supplierClaimUplaod.Error;
                            ViewBag.SystemCost = supplierClaimUplaod.SystemCost;

                            ViewBag.Confirmation = supplierClaimUplaod.Error +
                                                   supplierClaimUplaod.OrigInv +
                                                   supplierClaimUplaod.ST_Key +
                                                   supplierClaimUplaod.SupplierClaim +
                                                   supplierClaimUplaod.SystemCost;

                            return View(supplierClaimsData);
                        }

                        conn.Close();

                    }

                }
                catch (Exception)
                {

                    throw;
                }
            }



            return View(supplierClaimsData);
        }

    }
}

我设法将控制器响应传递给对话框,我正在努力触发循环以返回控制器并完成所有记录。当用户点击Accept Record我想回到控制器,并且可能在某处保存记录(sql)并验证下一条记录等等,我基本上问我如何连接Jquery按钮点击Accept或Reject返回控制器并根据用户选择的按钮运行其他代码。

1 个答案:

答案 0 :(得分:0)

您可以通过对控制器操作的ajax调用来执行此操作。

$.ajax({
    url: "@Url.Action("YourAction", "YourController")",
    data: some_parameters
    type: "POST",
    success: function(response){ },
    error: function(jqXHR, textStatus, errorThrown) { }
});

Here了解更多详情