我从数据库中提取数据以显示在网页上;但是当我输入四次或更多次时,数据是重复的。即它显示了输入的相同项目的四个不同集合和数据库;它没有表现出来。在调试中我认识到这个代码行items.Add(item);不断重复,但不确定要改变什么。 enter image description here这是我目前的输出。我想要的时间让申请单上的项目显示一次。这是执行程序enter image description here的结果;在req号码上输入了四个项目;但是如果你看一下这张照片enter image description here,你会看到它有12次,这意味着它会倍增3次。
控制器
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>
<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>
<br /><br /><br
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;
}