【发布时间】:2011-11-19 02:00:42
【问题描述】:
我是 ASP.NET MVC 的新手,需要一些帮助。
当 post 触发“P01”动作时,我收到强类型模型作为参数,更改此模型实例的属性并调用“return View(model);”。
在视图中使用“@Html.TextBoxFor(m => m.p01campo01)”语法。
有人 here 遇到了类似的问题,并获得了使用我正在使用的语法的建议。
有人 here 正在使用 "" 语法。
问题是当视图呈现文本框有最后发布的值,而不是我在控制器中分配的值。
感谢所有在这里尝试帮助我的人,我将第一个有效的答案作为答案。
这是我的代码:
*************************************************
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>
}
*************************************************
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 razor