我有一个名为HOLIDAY_DATE_TABLE的表。当用户通过我的应用程序在表中创建新记录时,ACTION_TYPE字段被赋值为“ADD”。当用户更新行时,如果当前ACTION_TYPE值不等于“ADD”,我希望ACTION_TYPE字段包含值“CHANGE”。我试图解决的问题是,当你在我的应用程序中创建一个新行时,它应该始终具有ACTION_TYPE值“ADD”。但是,当我更新通过我的应用程序创建的那一行时,它会更改为“CHANGE”。我已尝试使用下面的代码修复此问题,但在这种情况下,值仍会更新为“CHANGE”。
控制器
public ActionResult Edit(int id = 0)
{
HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
if (holiday_date_table == null)
{
return HttpNotFound();
}
return View(holiday_date_table);
}
//
// POST: /Table8/Edit/5
[HttpPost]
public ActionResult Edit(HOLIDAY_DATE_TABLE holiday_date_table)
{
if (ModelState.IsValid)
{
var somevar = holiday_date_table.HID.ToString();
string aconnectionString1 = "Data Source=SWDB10DSQL;Initial Catalog=BillingUI;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";
string checkgroup1 = "SELECT ACTION_TYPE FROM HOLIDAY_DATE_TABLE WHERE HID = @ACTION";
using (SqlConnection _mycon = new SqlConnection(aconnectionString1))
{
using (SqlCommand mycmd = new SqlCommand(checkgroup1, _mycon))
{
_mycon.Open();
mycmd.Parameters.Add("@ACTION", SqlDbType.NChar).Value = somevar;
string checkresult = (string)mycmd.ExecuteScalar();
//cmd31.Parameters.Add("@SECURITY_GROUP", SqlDbType.VarChar).Value = "";
//string checkresult = (string)mycmd.ExecuteScalar();
if (checkresult == "ADD")
{
db.Entry(holiday_date_table).State = EntityState.Modified;
db.SaveChanges();
}
else
{
holiday_date_table.ACTION_TYPE = "CHANGE";
db.Entry(holiday_date_table).State = EntityState.Modified;
db.SaveChanges();
}
}
}
}
return RedirectToAction("../Billing/HolidayDateTable");
}
查看
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<br />
@*<div class="editor-label">
@Html.LabelFor(model => model.HID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.HID)
@Html.ValidationMessageFor(model => model.HID)
</div>
*@
@Html.HiddenFor(model => model.HID)
<div class="editor-label">
@Html.LabelFor(model => model.TABLE_NUMBER)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TABLE_NUMBER)
@Html.ValidationMessageFor(model => model.TABLE_NUMBER)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.HOLIDAY_DATE)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.HOLIDAY_DATE)
@Html.ValidationMessageFor(model => model.HOLIDAY_DATE)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FIELD_DESCRIPTION)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FIELD_DESCRIPTION)
@Html.ValidationMessageFor(model => model.FIELD_DESCRIPTION)
</div>
<br />
<p>
<input type="submit" value=" Update " />
</p>
}
或许可以澄清一些事情。我需要阻止ACTION_TYPE值为“ADD”的行更新为“CHANGE”。
更新:我设置了一个断点,发现somevar正在获取正确的id,而checkresult的值为“ADD”(带空格),所以我将if语句更改为
if (checkresult == "ADD ")
但是它仍然会将值从ADD更改为CHANGE。我迷路了。