Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

c# - View not updating after post

I have a controller method CreateOrUpdate, this method is supposed to save the car to the database and then return as normal.

public ActionResult CreateOrUpdate(int ID = 0)
{
    Car car = new Car(ID);
} 

[HttpPost]
public ActionResult CreateOrUpdate(Car car)
{
       car.Save();
       return View(car);
}

In the theCar.Save() method, i set the id for the car, with whatever the id will be in the database after the car is saved (When doing an insert I use SCOPE_IDENTITY(), the save method works well, and if i debug and watch the values for car after the Save() is called, the id is correct. But when the View is rendered the ID is 0 in the view.

Could anyone please help me,and tell me why this would happen. Am I not suppose to change the Model for the view in the HTTP POST method ? Should i Rather redirect to the original CreateOrUpdate() method if the save was successful.

question from:https://stackoverflow.com/questions/9645479/view-not-updating-after-post

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

it should be the ModelState problem. if you use Htmlhelper to Display id value. Default HtmlHelper display ModelState value not Model. Try display model value in view

<td>
    @Model.id
</td>

or Clean ModelState Value in controller

ModelState.Clear();

or reset id value after SaveChange.

theCar.Save();
ModelState["id"].Value = theCar.id
return View(theCar);

Reset the value of textarea after form submission


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...