在Asp.Net MVC视图中,不要渲染控制器所做的模型更改 [英] In Asp.Net MVC view dont render changes in model made by controller
问题描述
我是ASP.NET MVC的新手,需要一些帮助.
当帖子触发"P01"动作时,我收到强类型模型作为参数,请更改此模型实例的属性,然后调用"return View(model);".
在视图中,即时通讯使用"@ Html.TextBoxFor(m => m.p01campo01)"语法.
此处遇到了类似的问题,并获得了使用im语法的建议.
有人此处使用的是<%= Html.TextBox(" Person.LastName,ViewData.Model .LastName)%>语法.
问题是,当呈现视图时,文本框具有最后发布的值,而不是我在控制器中分配的值.
Im new to ASP.NET MVC and need some help.
When "P01" action is fired by post i receive the strongly typed model as parameter, change properties of this model instance and call "return View(model);".
In view im using "@Html.TextBoxFor(m => m.p01campo01)" syntax.
Someone here had a similar problem and get advice to use the syntax im using.
Someone here is using "<%=Html.TextBox("Person.LastName", ViewData.Model.LastName)%>" syntax.
The problem is when the view is rendered the textbox have the last posted value, not the value i assigned in controller.
感谢所有在这里帮助过我的人,我将第一个可行的答案作为答案.
Thanks for everyone that have tried to help me here, i put as answer the first answer that work.
有我的代码:
*************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace MyTest.Models
{
//-------
public class testePg01
{
[DataType(DataType.Text)]
[Display(Name = "p01campo01")]
public string p01campo01 { get; set; }
[DataType(DataType.Text)]
[Display(Name = "p01campo02")]
public string p01campo02 { get; set; }
}
//-------
}
*************************************************
*************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyTest.Models;
namespace MyTest.Controllers
{
public class TesteController : Controller
{
//-------
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult P01()
{
return View();
}
//-------
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult P01(testePg01 model)
{
model.p01campo01 = model.p01campo01 + "#updatedHereButNotInView";
model.p01campo02 = model.p01campo02 + "#updatedHereButNotInView";
return View(model); // it dont return updated
}
//-------
}
}
*************************************************
*************************************************
@model MyTest.Models.testePg01
@{
ViewBag.Title = "P01";
}
<h2>P01</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Html.ValidationSummary(true, "Erro na pagina.")
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div>
<fieldset>
<legend>Test P01</legend>
<div class="editor-label">
@Html.LabelFor(m => m.p01campo01)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.p01campo01)
@Html.ValidationMessageFor(m => m.p01campo01)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.p01campo02)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.p01campo02)
@Html.ValidationMessageFor(m => m.p01campo02)
</div>
<p>
<input type="submit" value="P01" />
</p>
</fieldset>
</div>
}
*************************************************
推荐答案
因为它是HttpPost,HTML助手会先查找已发布的数据,然后再查找模型.
Because it's an HttpPost HTML helpers are looking first to the POSTed data and only after that to the model.
您必须从状态中删除该属性:
You've to remove the property from the state:
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult P01(testePg01 model)
{
ModelState.Remove("p01campo01");
ModelState.Remove("p01campo02");
model.p01campo01 = model.p01campo01 + "#updatedHereButNotInView";
model.p01campo02 = model.p01campo02 + "#updatedHereButNotInView";
return View(model);
}
因为ModelState是一个字典,所以您也可以使用Clear()
从状态中删除所有属性:
Because ModelState is a dictionary you can also use Clear()
to remove the all properties from the state:
ModelState.Clear();
顺便说一句,在MVC3上,您可以使用[HttpPost]
和[HttpGet]
插入的[Acceptverb(...)]
BTW, on MVC3 you can use [HttpPost]
and [HttpGet]
insted of [Acceptverb(...)]
这篇关于在Asp.Net MVC视图中,不要渲染控制器所做的模型更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!