发帖后查看不更新 [英] View not updating after post

查看:16
本文介绍了发帖后查看不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器方法 CreateOrUpdate,这个方法应该将汽车保存到数据库,然后正常返回.

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);
}

在 theCar.Save() 方法中,我为汽车设置了 id,无论汽车保存后数据库中的 id 是什么(在执行插入时我使用 SCOPE_IDENTITY(),保存方法运行良好, 如果我在调用 Save() 后调试并观察 car 的值,则 id 是正确的.但是当视图呈现时,视图中的 ID 为 0.

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.

谁能帮助我,告诉我为什么会发生这种情况.我不应该在 HTTP POST 方法中更改视图的模型吗?如果保存成功,我应该重定向到原始的 CreateOrUpdate() 方法吗?

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.

推荐答案

应该是ModelState问题.如果您使用 Htmlhelper 来显示 id 值.默认 HtmlHelper 显示 ModelState 值而不是 Model.尝试在视图中显示模型值

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>

或清除控制器中的 ModelState 值

or Clean ModelState Value in controller

ModelState.Clear();

或在 SaveChange 后重置 id 值.

or reset id value after SaveChange.

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

表单后重置textarea的值提交

这篇关于发帖后查看不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆