簡體   English   中英

在ASP.NET MVC中復制數據

[英]Duplication of Data in asp.net mvc

我正在使用存儲過程從數據庫中提取項目列表,但是在提取數據時,它會多次顯示項目。 假設我輸入了4個項目,然后在頁面上顯示了4個不同的集合,如果是兩個項目,則顯示兩個不同的集合,如果是3個項目,則顯示2個集合。 我看不出為什么要這么做:

在此處輸入圖片說明

控制者

public ActionResult RequisitionList(List<Requisition> postingObj)
{
        IssueDAO dbObj = new IssueDAO(ConfigurationManager.ConnectionStrings["TWCL_OPERATIONSConnectionString"].ConnectionString);
        List<string> reqNumbers = new List<string>();

        if (postingObj == null)
        {
            ViewBag.Message = "There is no transaction to be posted";
            return View(dbObj.GetAllRequest());
        }

        foreach (var item in postingObj)
        {
            if (item.postTrnx)
            {
                reqNumbers.Add(item.reqNumber);
            }
        }

        if (reqNumbers.Count == 0)
        {
            ViewBag.Message = "Please select at least one request.";
            return View(dbObj.GetAllRequest());
        }

        dbObj.SetRequisitionStatus0(reqNumbers);
        ViewBag.Message = "Approval Successful!";

        return View(dbObj.GetAllRequest());
}

public ActionResult RequisitionList()
{
        List<Requisition> issuesListOb = new List<Requisition>();
        IssueDAO dbObj = new IssueDAO();
        dbObj.connectionString = ConfigurationManager.ConnectionStrings["TWCL_OPERATIONSConnectionString"].ConnectionString;
        issuesListOb = dbObj.GetAllRequest();

        return View(issuesListOb);
}

視圖

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div>
        <hr />
    </div>

    <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                @*<input type="submit" value="Issue" name="Issue" class="btn btn-default" />*@
                @*<input type="submit" value="Search" name="Search" class="btn btn-default" />*@

            </div>
    </div>

    <table id="data">
        <thead>
            <tr>
                <th class="col-lg-1">@Html.CheckBox("TheOneCheckBoxToRuleThemAll")Select All</th>
                <th class="col-lg-1 ">Date</th>
                <th class="col-lg-1 ">Requisition Number</th>
                <th class="col-lg-1 ">Expense Account</th>
                <th class="col-lg-1">Requestor</th>
                <th class="col-lg-1">Department</th>
                <th class="col-lg-1">LoggedinAs</th>
                <th class="col-lg-1 ">Item Number</th>
                <th class="col-lg-1 ">Description</th>
                <th class="col-sm-1">Quantity</th>
                <th class="col-sm-1 ">UOM</th>
            </tr>
        </thead>
        <tbody>
        @for (int i = 0; i < Model.Count; i++)
        {
                    @Html.HiddenFor(m => m[i].reqNumber)
                    @Html.HiddenFor(m => m[i].department)
                    @Html.HiddenFor(m => m[i].department)

                    @*<tr>
                        <td>@Html.CheckBoxFor(m => m[i].postTrnx, new { @class = "checkGroup1" })</td>
                        <td class="label">
                            @Html.DisplayFor(m => m[i].reqNumber)
                            @Html.DisplayFor(m => m[i].reqDate)
                        </td>
                    </tr>*@
                    foreach (var item in Model[i].items)
                    {
                        @Html.HiddenFor(m => item.description)
                            @Html.HiddenFor(m => item.expense_account)
                            @Html.HiddenFor(m => item.itemNumber)
                        <tr>
                            <td>@Html.CheckBoxFor(m => m[i].postTrnx, new { @class = "checkGroup1" })</td>
                            <td class="col-lg-1 tabledata" >@item.requisition.reqDate</td>
                            <td class="col-lg-1 tabledata" >@item.requisition.reqNumber</td>
                            <td class="col-lg-1 tabledata">@item.expense_account.account_desc</td>
                            <td class="col-lg-1 tabledata">@item.employeeDetails.employeeNum</td>
                            <td class="col-lg-1 tabledata">@item.employeeDetails.department</td>
                            <td class="col-lg-1 tabledata">@item.employeeDetails.LoggedInUserName</td>
                            <td class="col-lg-1 tabledata">@item.itemNumber</td>
                            <td class="col-lg-1 tabledata">@item.description</td>
                            <td class="col-sm-1 tabledata">@item.quantity</td>
                            <td class="col-sm-1 tabledata">@item.selecteduomtext </td>

                            @*<td>@Html.ActionLink("Edit", "Edit", new { id = @item.lineNum, name = Model[i].reqNumber })</td>*@
                        </tr>
                    }
                }
            </tbody>
        </table>

模型

public List<Requisition> GetAllRequest()
{
        using (var connection = new SqlConnection(connectionString))
        {
            using (var command = new SqlCommand("getallrequests", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                List<Requisition> request = new List<Requisition>();

                SqlDataReader rdrObj;

                connection.Open();
                rdrObj = command.ExecuteReader();

                while (rdrObj.Read())
                {
                    Requisition requisition = new Requisition();

                    requisition.reqNumber = rdrObj.GetString(0);
                    requisition.reqDate = rdrObj.GetDateTime(1);

                    requisition.items = getRequestItemByRquisition(rdrObj.GetString(4));
                   request.Add(requisition);
                }

                rdrObj.Close();

                return request;
            }
        }
}

public List<Item> getRequestItemByRquisition(string Req_No)
{
        List<Item> items = new List<Item>();
        SqlConnection TWCLOPConnect = new SqlConnection(connectionString.ToString());

        SqlCommand itemscommand = new SqlCommand();
        SqlDataReader itemRdr;

        itemscommand.CommandText = "requisition_sp_getItemNum ";
        itemscommand.CommandType = CommandType.StoredProcedure;
        itemscommand.Connection = TWCLOPConnect;
        itemscommand.Parameters.Add("@Req_No", SqlDbType.VarChar).Value = Req_No;

        try
        {
            TWCLOPConnect.Open();
            itemRdr = itemscommand.ExecuteReader();

            while (itemRdr.Read())
            {
                Item item = new Item();
                item.itemNumber = itemRdr.GetString(0);
                item.description = itemRdr.GetString(1);
                item.price = Convert.ToDouble(itemRdr[3]);
                item.quantity = Convert.ToDouble(itemRdr[4]);
                item.expense_account.index = itemRdr.GetInt32(5);
                item.expense_account.account_desc = itemRdr.GetString(6);

                item.selecteduomtext = itemRdr.GetString(8);
                items.Add(item);
            }

            itemRdr.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            TWCLOPConnect.Close();
        }

        return items;
}

是由於多個。
@ Html.HiddenFor(m => m [i] .department)在您看來?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM