我有一个 ASP.Net MVC2 应用程序,它有一个带有文本框的表格,用户可以在其中输入评论并单击更新以更新单行数据。表格的每一行都是通过基于用户输入的日期调用 db 并使用 for each 循环生成的,并用名称包装在一个表单中commentaryForm
- 像这样
<% foreach (var item in Model)
using (Html.BeginForm("Commentary", "Home", FormMethod.Post, new { name = "commentaryForm", id = "commentaryForm" }))
{ %>
<tr>
<td><%= Html.TextBox("comDate", item.aReportingDate.ToString().Substring(0, 10), new { @readonly = true, @class = "readOnlyTBox" })%></td>
<td><%= Html.Encode(item.BPM)%></td>
<td><%= Html.Encode(item.criticality)%></td>
<td><%= Html.Encode(item.team)%></td>
<td><%= Html.Encode(Convert.ToInt32(item.newIn))%></td>
<td><%= Html.Encode(Convert.ToInt32(item.outsideSLA))%></td>
<td><%= Html.TextArea("why", item.commentaryWhy, new { @class = "commentaryInput" })%></td>
<td><%= Html.TextArea("what", item.commentaryWhat, new { @class = "commentaryInput" })%></td>
<td><%= Html.TextBox("when", item.commentaryWhen, new { @class = "when" })%></td>
<td id="resultsTd"><input type="submit" class="submitButton noMargin" value="Update" /></td>
<td class=""><%= Html.Encode(item.commentaryId)%> <%= Html.TextBox("id", item.commentaryId, new { @class = "when", type = "", value = item.commentaryId })%></td>
</tr>
<% }%>
单击连续更新会调用此控制器,其中数组包含来自该行中单个文本框/文本区域集合的值。
public ActionResult Commentary(string[] why, string[] what, DateTime[] when, int[] id)
{
var reportingDate = new DateTime();
reportingDate = Convert.ToDateTime(Request.Form["comDate"]);
if (reportingDate.ToString().Substring(0, 10).Equals(@"01/01/0001"))
{
DateTime latestDay = DateTime.Now.AddDays(-1);
reportingDate = latestDay;
ViewData["start"] = latestDay.ToString().Substring(0, 10);
}
else
ViewData["start"] = reportingDate.ToString().Substring(0, 10);
var model = new List<commentaryVModel>();
if (why != null && what != null && when != null && id != null)
{
DataLayer.Updatedata(id, why, what, when);
}
model = DataLayer.viewCommentary(model, reportingDate);
return View(model);
}
DataLayer.Updatedata
并且DataLayer.viewCommentary(model, reportingDate)
似乎在逐步完成时工作正常,并且值符合预期。
问题是当您单击更新后页面重新加载时,所有表行中的值都已被您更新的行中的值替换!仅在用户看到的 DOM 中,而不是在数据库中!诡异的!
任何人都可以帮忙吗?谢谢!我已经在下面发布了数据库方法的代码,我还不能发布屏幕截图,因为这是我的第一个问题。
public static List<commentaryVModel> viewCommentary(List<commentaryVModel> model, DateTime reportingDate)
{
SqlConnection con = DataLayer.getConnection();
SqlDataReader reader = null;
string commandString = DataLayer.Commentary();
SqlCommand command = new SqlCommand(commandString, con);
command.CommandType = CommandType.StoredProcedure;
// add parameters
command.Parameters.AddWithValue("@reportingDate", reportingDate);
try
{
// execute a reader with the command
reader = command.ExecuteReader();
{
// loop in the result and fill the list
while (reader.Read())
{
// add items in the list
model.Add(new commentaryVModel()
{
BPM = reader["BPM"] as string,
aReportingDate = (DateTime)reader["date"],
team = reader["Team"] as string,
criticality = reader["Criticality"] as string,
newIn = (decimal)reader["NewIn"],
outsideSLA = (decimal)reader[@"OutsideSLA"],
commentaryId = (int)reader[@"Id"],
commentaryWhy = reader[@"CommentaryWhy"] as string,
commentaryWhat = reader[@"CommentaryWhat"] as string,
commentaryWhen = reader[@"CommentaryWhen"] as string
});
}
}
}
catch
{ }
finally
{
// 3. close the reader
if (reader != null)
{
reader.Close();
}
// close the connection
if (con != null)
{
con.Close();
}
}
public static void Updatedata(int[] id, string[] why, string[] what, DateTime[] when)
{
SqlConnection con = DataLayer.getConnection();
try
{
string fullString = null;
// prepare command string
for (int i = 0; i < id.Length; i++)
{
string updateString = @"
UPDATE tCommentary
SET CommentaryWhy = '" + why[i] + "'" +
",CommentaryWhat = '" + what[i] + "'" +
",CommentaryWhen = '" + when[i] + "'" +
"WHERE rowId = " + id[i] + " ";
fullString = fullString + " " + updateString;
}
// 1. Instantiate a new command with a query and connection
SqlCommand cmd = new SqlCommand(fullString, con);
// 2. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
}
catch
{ }
finally
{
// Close the connection
if (con != null)
{
con.Close();
}
}
}