恳请高手。帮帮小弟。小弟不知什么原因。。好像代码都写正确了。。但是,修改记录时,就是不能成功。
输入新的数据修改时,查看数据库时,还是原来的那条记录。。先谢谢各路看贴的高手们先了....htm代码:
<h2>
update</h2>
<%using (Html.BeginForm())
{ %>
<fieldset>
<legend>修改信息</legend>
<p>
<label for="name">
目录名:</label>
<%=Html.TextBox("CategroyName", Model.CategroyName)%>
<%=Html.ValidationMessage("name")%>
</p>
<p>
<label for="discrible">
描述:</label>
<%=Html.TextBox("Description", Model.Description)%>
<%=Html.ValidationMessage("discrible")%>
</p>
<p>
<input type="submit" value="Update" />
</p>
</fieldset>
<%} %>.cs文件代码:[AcceptVerbs(HttpVerbs.Post)]
public ActionResult update(int? id, Mvc_Product.Models.Catgory catgory)
{
bool check = true;
if(string.IsNullOrEmpty(catgory.CategroyName))
{
ModelState.AddModelError("CN","目录名不能为空....");
check = false;
}
if(string.IsNullOrEmpty(catgory.Description))
{
ModelState.AddModelError("Desc", "描述也不能为空呀....");
check = false;
}
if (!check) return View(catgory); if (id != 0&&id!=null)
{ var originalCatgory = (from m in dbdc.Catgory
where m.CategoryID ==id
select m).First();
//如果这些验证没有通过了。
if (!ModelState.IsValid) return View(originalCatgory); originalCatgory.CategroyName = catgory.CategroyName;
originalCatgory.Description = catgory.Description;
}
else
{
dbdc.Catgory.InsertOnSubmit(catgory);
}
dbdc.SubmitChanges();
return RedirectToAction("update");
}
输入新的数据修改时,查看数据库时,还是原来的那条记录。。先谢谢各路看贴的高手们先了....htm代码:
<h2>
update</h2>
<%using (Html.BeginForm())
{ %>
<fieldset>
<legend>修改信息</legend>
<p>
<label for="name">
目录名:</label>
<%=Html.TextBox("CategroyName", Model.CategroyName)%>
<%=Html.ValidationMessage("name")%>
</p>
<p>
<label for="discrible">
描述:</label>
<%=Html.TextBox("Description", Model.Description)%>
<%=Html.ValidationMessage("discrible")%>
</p>
<p>
<input type="submit" value="Update" />
</p>
</fieldset>
<%} %>.cs文件代码:[AcceptVerbs(HttpVerbs.Post)]
public ActionResult update(int? id, Mvc_Product.Models.Catgory catgory)
{
bool check = true;
if(string.IsNullOrEmpty(catgory.CategroyName))
{
ModelState.AddModelError("CN","目录名不能为空....");
check = false;
}
if(string.IsNullOrEmpty(catgory.Description))
{
ModelState.AddModelError("Desc", "描述也不能为空呀....");
check = false;
}
if (!check) return View(catgory); if (id != 0&&id!=null)
{ var originalCatgory = (from m in dbdc.Catgory
where m.CategoryID ==id
select m).First();
//如果这些验证没有通过了。
if (!ModelState.IsValid) return View(originalCatgory); originalCatgory.CategroyName = catgory.CategroyName;
originalCatgory.Description = catgory.Description;
}
else
{
dbdc.Catgory.InsertOnSubmit(catgory);
}
dbdc.SubmitChanges();
return RedirectToAction("update");
}
dbdc.SaveChanges();
你的CategoryId没有传递到Model中,你的Model对象既然是从form提交而来,那么CategoryId也必须在这个form中,解决办法就是在form中加个隐藏文本框记录CategoryId:<%=Html.Hidden("CategoryID",Model.CategoryId)%>我按照 你的方法加了隐藏将ID值传了过去。。但是还是不可以成功更改数据.....
断点调试时,看到传过来的值了但是,还是修改不成功呀。。这是为什么呢?
小弟不知为什么,,,再恳请高手帮帮小弟谢谢您。们。。