更新ASP.NET MVC和LINQ到SQL行 [英] Update a row in ASP.NET and MVC LINQ to SQL
问题描述
我有一个简单的行有4列:
I have a simple row that has 4 columns:
{ [Primary Key Int]RowID, [text]Title, [text]Text, [datetime]Date }
我想允许用户具有与田标题和文本。
I would like to allow the user to edit this row on a simple page that has a form with the fields "Title" and "Text".
有一个隐藏字段来存储rowid的。
There is a hidden field to store the RowID.
当用户帖子这种形式对我控制器操作,我想它更新的行的标题和文本,并保持数据的一致。我不希望有明确包含一个隐藏字段在表单页面的日期。
When the user posts this form to my controller action, I want it to update the row's Title and Text, and keep the Date the same. I don't want to have to explicitly include a hidden field for the Date in the form page.
下面是我的行动:
[AcceptVerbs(HttpVerb.Post)]
public ActionResult EditRow(Row myRow)
{
RowRepository.SaveRow(myRow)
return View("Success");
}
RowRepository:
RowRepository:
public void SaveRow(Row myRow)
{
db.MyRows.Attach(myRow);
db.Refresh(RefreshMode.KeepCurrentValues, myRow);
db.SubmitChanges();
}
这这么想的保持日期值已经行中,并试图插入抛出异常的时间跨度值。
This dosen't keep the "Date" value already in the row and tries to insert a value that throws an timespan exception.
如何我只是告诉它保留旧的价值观?
我试着做 RefreshMode.KeepChanges
并没有什么。
How can I just tell it to keep the old values?
I tried doing RefreshMode.KeepChanges
and nothing.
推荐答案
好吧,我将它设置可空,并不断覆盖数据库作为空值。我猜它不可能做到这一点,因为在技术上空是列的有效值,如果我传递一个对象的功能,空值必须包含的东西或为空。
Ok, I set it to nullable and it keeps overwriting the database as a null value. I guess its impossible to do this since technically null is a valid value for the column and if I pass an object to the function, the empty values must contain something or be null.
所以,我必须明确指出需要对数据库值列
So I would have to explicitly state to take the database value for that column
感谢
这篇关于更新ASP.NET MVC和LINQ到SQL行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!