【发布时间】:2025-12-07 20:30:01
【问题描述】:
Asp.Net MVC 3
我似乎遇到了与 Darin Dimitrov 回答的这篇文章类似的问题。所以,达林,如果你正在阅读这篇文章,请帮忙:)
asp.net-mvc2 - Strongly typed helpers not using Model?
我遇到的问题是我正在寻找一个包含模型状态中发布的值的 html 助手。
例如,如果我使用这样的编辑器:
@Html.EditorFor(model => model.SelectedTags)
我可以看到发布的值。问题是我需要一种在不创建文本框的情况下获取此值的方法,我只想要字符串,因为我需要在一些 javascript 中使用它。
我尝试过 DisplayFor,但它不包含发布的值:
@Html.DisplayFor(model => model.SelectedTags)
顺便说一句,我觉得这种行为一点也不直观。我花了几个小时从 MVCContrib 调试 ModelStateToTempDataAttribute,认为这是他们导入/导出模型状态的代码中的错误。
感谢您的帮助!
编辑 - 添加重现代码
按照以下步骤重现:
- 启动项目。 Property1 应为空白(必需),Property2 应具有“abc”
- 将 Property2 更改为“xxx”
- 提交表单(注意 ClientValidationEnabled 为 False)
- 表单已发布、重定向、加载 (PRG)。 Property2 文本框有“xxx”,您将在 DisplayFor 的正下方看到“abc”。
控制器
[ModelStateToTempData] //From MVCContrib
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
//simulate load from db
var model = new FormModel() { MyProperty2 = "abc" };
return View(model);
}
[HttpGet]
public ActionResult Success()
{
return View();
}
[HttpPost]
public ActionResult Index(FormModel model)
{
if (ModelState.IsValid)
{
return RedirectToAction("Success");
}
else
{
return RedirectToAction("Index");
}
}
}
型号
public class FormModel
{
[Required]
public string MyProperty1 { get; set; }
public string MyProperty2 { get; set; }
}
查看
@model MvcApplication4.Models.FormModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>FormModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.MyProperty1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MyProperty1)
@Html.ValidationMessageFor(model => model.MyProperty1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyProperty2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MyProperty2)
@Html.ValidationMessageFor(model => model.MyProperty2)
</div>
@Html.DisplayFor(model => model.MyProperty2)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
配置:
<add key="ClientValidationEnabled" value="false" />
ModelStateToTempData (MVCContrib):
public class ModelStateToTempDataAttribute : ActionFilterAttribute
{
public const string TempDataKey = "__MvcContrib_ValidationFailures__";
/// <summary>
/// When a RedirectToRouteResult is returned from an action, anything in the ViewData.ModelState dictionary will be copied into TempData.
/// When a ViewResultBase is returned from an action, any ModelState entries that were previously copied to TempData will be copied back to the ModelState dictionary.
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var modelState = filterContext.Controller.ViewData.ModelState;
var controller = filterContext.Controller;
if(filterContext.Result is ViewResultBase)
{
//If there are failures in tempdata, copy them to the modelstate
CopyTempDataToModelState(controller.ViewData.ModelState, controller.TempData);
return;
}
//If we're redirecting and there are errors, put them in tempdata instead (so they can later be copied back to modelstate)
if((filterContext.Result is RedirectToRouteResult || filterContext.Result is RedirectResult) && !modelState.IsValid)
{
CopyModelStateToTempData(controller.ViewData.ModelState, controller.TempData);
}
}
private void CopyTempDataToModelState(ModelStateDictionary modelState, TempDataDictionary tempData)
{
if(!tempData.ContainsKey(TempDataKey)) return;
var fromTempData = tempData[TempDataKey] as ModelStateDictionary;
if(fromTempData == null) return;
foreach(var pair in fromTempData)
{
if (modelState.ContainsKey(pair.Key))
{
modelState[pair.Key].Value = pair.Value.Value;
foreach(var error in pair.Value.Errors)
{
modelState[pair.Key].Errors.Add(error);
}
}
else
{
modelState.Add(pair.Key, pair.Value);
}
}
}
private static void CopyModelStateToTempData(ModelStateDictionary modelState, TempDataDictionary tempData)
{
tempData[TempDataKey] = modelState;
}
}
【问题讨论】:
-
DisplayFor是正确的方法。检查您的QueryString以查看是否有设置为SelectedTags的值,我敢肯定`首先显示在实际模型值之上。 -
@BuildStarted 没有查询字符串。我有一个包含 EditorFor 和 DisplayFor 的测试页面,只有 EditorFor 显示发布的值。
-
您能否发布您的代码(查看模型、操作方法和视图)? DisplayFor 应该可以胜任。
-
@BuildStarted 代码添加