asp.net-mvc – 保存后显示相同的页面
发布时间:2021-03-30 12:07:38 所属栏目:asp.Net 来源:互联网
导读:我想显示一个带有某个字段的表单(示例中有一个),提交它,保存并显示同一页面并重置所有字段.我提交时的问题,我进行“保存”操作,但是当我显示视图时,表单仍然填写. 该模型 : public class TestingModel{ public string FirstName { get; set; }} 控制器: pub
|
我想显示一个带有某个字段的表单(示例中有一个),提交它,保存并显示同一页面并重置所有字段.我提交时的问题,我进行“保存”操作,但是当我显示视图时,表单仍然填写. 该模型 : public class TestingModel
{
public string FirstName { get; set; }
}
控制器: public class ChildController : Controller
{
public ActionResult Index()
{
TestingModel model = new TestingModel();
return View(model);
}
public ActionResult Save(TestingModel model)
{
Console.WriteLine(model.FirstName); //OK
//Save data to DB here ...
TestingModel testingModel = new TestingModel() { FirstName = string.Empty };
return View("Index",testingModel);
}
}
风景 : @using (Html.BeginForm("Save","Child",FormMethod.Post))
{
@Html.TextBoxFor( m => m.FirstName)
<input type="submit" id="btSave" />
}
当Id调试到视图时,在“Immediat窗口”Model.FirstName =“”但是当页面显示时,我仍然发布了值.我在Save方法的末尾尝试了ReditrectionToAction(“Index”)但结果相同. 你有好主意吗 ? 谢谢, 解决方法如果你想这样做,你需要清除ModelState中的所有内容.否则,HTML帮助程序将完全忽略您的模型,并在绑定其值时使用ModelState中的数据.像这样: [HttpPost]
public ActionResult Save(TestingModel model)
{
//Save data to DB here ...
ModelState.Clear();
TestingModel testingModel = new TestingModel() { FirstName = string.Empty };
return View("Index",testingModel);
}
或者只是在成功的情况下重定向到Index GET操作: [HttpPost]
public ActionResult Save(TestingModel model)
{
//Save data to DB here ...
return RedirectToAction("Index");
} (编辑:东莞站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- LoginView中的ASP.NET LoginStatus不会触发LoggingOut事件
- asp.net-mvc – 使用IIS7.5将网站中的URL绑定到另一个网站控
- 如何通过Asp.net WebAPI中的异常过滤器传递内容?
- asp.net – 转发器控件中的单选按钮列表
- 详解Asp.net Core 使用Redis存储Session
- asp.net-mvc – 无法更改关系,因为一个或多个外键属性在MVC
- asp.net-mvc – ASP.NET MVC如何在生产中禁用调试路由/视图
- asp.net-mvc – 在Asp.Net MVC中使用千位分隔符的十进制值
- asp.net – 如何扩展aspnet成员身份验证表?
- asp.net-mvc-3 – 具有最佳实践的示例N层ASP.NET MVC3应用程
