Update a row in ASP.NET and MVC LINQ to SQL

2019-07-24 18:40发布

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".

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.

Here is my action:

[AcceptVerbs(HttpVerb.Post)]
public ActionResult EditRow(Row myRow)
{
    RowRepository.SaveRow(myRow)   
    return View("Success");
}

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.

How can I just tell it to keep the old values? I tried doing RefreshMode.KeepChanges and nothing.

4条回答
唯我独甜
2楼-- · 2019-07-24 19:12

Try

[AcceptVerbs(HttpVerb.Post)]
public ActionResult EditRow([Bind(Exclude="Date")] Row myRow) {
  RowRepository.SaveRow(myRow)   
  return View("Success");
}

Update

Try this approach, where there is no 'Date' field on your page

[AcceptVerbs(HttpVerb.Post)]
public ActionResult EditRow(int RowID) {
  Row myRow = RowRepository.GetRow(RowID);
  UpdateModel(myRow);
  RowRepository.Save();
  return View("Success");
}

In your repository

public void Save() {
  db.SubmitChanges();
}

This will only save the changes made to 'myRow'

查看更多
做自己的国王
3楼-- · 2019-07-24 19:13

You will have add a method in the partial class / override the code it build.

The class Table does implement "INotifyPropertyChanging|ed" which is used to track which column has been changed.

You can hack it and reset the value "this.PropertyChanged".

But what I do at work is a stupid READ-APPLY-WRITE approach (and I am using WebForm).

public void SaveRow(Row myRow)
{
    var obj=db.MyRows.Where(c=>c.id==myRow.id).First();
    obj.a=myRow.a;
    obj.b=myRow.b;
    db.SubmitChanges();
}

You can do a bit simpler.

public void SaveRow(Row myRow)
{
    db.MyRows.Attach(new Row(){
        Id=myRow.Id,
        Title=myRow.Title,
        Text=myRow.Text,
    });
    db.SubmitChanges();
}

PS. I am new to LINQ to SQL. Please let me know if there is a smarter way to do it.

查看更多
仙女界的扛把子
4楼-- · 2019-07-24 19:26

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

Thanks

查看更多
smile是对你的礼貌
5楼-- · 2019-07-24 19:31

I'm not in a position to test this at the moment but try making the datetime column nullable and then ensure that the datetime passed into SaveRow has a null value.

查看更多
登录 后发表回答