asp.net-mvc – 我的模型中名为Title的属性与我视图中的View.Title之间的绑定冲突(在MVC中)
发布时间:2020-05-24 10:47:19 所属栏目:asp.Net 来源:互联网
导读:我的模型包含一个名为Title的属性,在我的Create视图中,我使用ViewBag.Title设置页面标题. 这会产生以下问题:Html.Editor生成的表单将显示ViewBag.Title中的文本,而不是模型的Title值. 我找到的唯一解决方法是首先调用Html.Editor,然后设置View.Title. 有没有
|
我的模型包含一个名为Title的属性,在我的Create视图中,我使用ViewBag.Title设置页面标题. 这会产生以下问题:Html.Editor生成的表单将显示ViewBag.Title中的文本,而不是模型的Title值. 我找到的唯一解决方法是首先调用Html.Editor,然后设置View.Title. 有没有人有更好的解决方案? 编辑1:我正在使用MVC 3. 编辑2:这是我的DisplayTemplates / Object.cshtml: @model dynamic
@using Iconum.VS10CS040.Library.Web.MVC3.Helpers
@if (ViewData.TemplateInfo.TemplateDepth > 1) {
<span class="editor-object simple">@ViewData.ModelMetadata.SimpleDisplayText</span>
} else {
foreach (var prop in ViewData.ModelMetadata.Properties.Where(
pm =>
pm.ShowForEdit
&& !ViewData.TemplateInfo.Visited(pm)
&& pm.ModelType != typeof(System.Data.EntityState)
&& !pm.IsComplexType
)
)
{
if (prop.HideSurroundingHtml) {
<text>@Html.Editor(prop.PropertyName)</text>
} else {
string css = "";
if (prop.Model != null && prop.Model.GetType() != null)
{
css += " " + prop.Model.GetType().ToString().ToLower().Replace('.','-');
}
if (prop.DataTypeName != null)
{
css += " " + prop.DataTypeName.ToLower();
}
if (prop.IsRequired && prop.ModelType.FullName != "System.Boolean")
{
css += " required";
}
<div class="editor-container @css">
<div class="editor-label">
@if (!String.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString()))
{
// Use LabelWithForThatMatchesTheIdOfTheInput instead of Label because of a bug (fixed in MVC 3)
@Html.LabelWithForThatMatchesTheIdOfTheInput(prop.PropertyName)
}
@if (prop.IsRequired && prop.ModelType.FullName != "System.Boolean")
{
@Html.Raw(" <span class="required">*<span>");
}
</div>
<div class="editor-field">
@* This the line that causes my problem *@
@Html.Editor(prop.PropertyName)
@Html.ValidationMessage(prop.PropertyName)
</div>
</div>
}
} //foreach
// Loop though all items in the Model with an TemplateHint (UIHint)
foreach (var prop in ViewData.ModelMetadata.Properties.Where(
pm => pm.ShowForEdit
&& !ViewData.TemplateInfo.Visited(pm)
&& pm.ModelType != typeof(System.Data.EntityState)
&& !pm.IsComplexType
&& pm.TemplateHint != null
&& (
pm.TemplateHint == "jWYSIWYG0093"
||
pm.TemplateHint == "jQueryUIDatepicker"
||
pm.TemplateHint == "CKEditor"
)
)
)
{
// TODO: check for duplicate js file includes
@Html.Editor(prop.PropertyName,prop.TemplateHint + "-Script")
}
}
解决方法我建议使用EditorFor而不是Editor.Html.EditorFor(x => x.Title) 代替: Html.Editor("Title")
这样不仅视图可以利用您的视图模型,而且在这种情况下它的行为也符合预期. ASP.NET MVC 3.0 RTM(Razor)的示例: 模型: public class MyViewModel
{
public string Title { get; set; }
}
控制器: public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Title = "ViewBag title";
ViewData["Title"] = "ViewData title";
var model = new MyViewModel
{
Title = "Model title"
};
return View(model);
}
}
视图: @model AppName.Models.MyViewModel
@{
ViewBag.Title = "Home Page";
}
@Html.EditorFor(x => x.Title)
@{
ViewBag.Title = "Some other title";
}
所以无论我们在这里滥用多少,编辑器模板都使用正确的模型标题(如果我们使用Html.Editor(“Title”)则不是这种情况). (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 选择下拉列表项目findbytext没有区分大小写vb.n
- asp.net-web-api – WebAPI 2.2不支持substringof函数
- asp.net-mvc – 在数据库表中存储用户筛选查询参数的最佳方
- asp.net – 什么原因导致“无法注销UpdatePanel”错误?
- asp.net TextBox中的值和文本属性(值被Text覆盖)
- ASP.NET错误处理
- asp.net – global.asax Application_Error不触发
- 在IIS上部署ASP.NET Core项目的图文方法
- 如何在ASP.NET MVC 5.2.3应用程序的其他地方获取IAppBuilde
- asp.net – 优雅地停止fastcgi-mono-server,网站内容更新,无
推荐文章
站长推荐
热点阅读
