我的模型中名为 Title 的属性与视图中的 View.Title 之间的绑定冲突(在 MVC 中) [英] Binding conflict between a property named Title in my Model and View.Title in my View (in MVC)

查看:33
本文介绍了我的模型中名为 Title 的属性与视图中的 View.Title 之间的绑定冲突(在 MVC 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型包含一个名为 Title 的属性,在我的 Create 视图中,我使用 ViewBag.Title 设置页面标题.

这会产生以下问题:Html.Editor 生成的表单将显示来自 ViewBag.Title 的文本,而不是模型的 Title> 价值.

我发现的唯一解决方法是先调用 Html.Editor,然后设置 View.Title.

有人有更好的解决方案吗?

编辑 1:我使用的是 MVC 3.

编辑 2:这是我的 DisplayTemplates/Object.cshtml:

@model 动态@using Iconum.VS10CS040.Library.Web.MVC3.Helpers@if (ViewData.TemplateInfo.TemplateDepth > 1) {<span class="editor-object simple">@ViewData.ModelMetadata.SimpleDisplayText</span>} 别的 {foreach (ViewData.ModelMetadata.Properties.Where(下午 =>pm.ShowForEdit&&!ViewData.TemplateInfo.Visited(pm)&&pm.ModelType != typeof(System.Data.EntityState)&&!pm.IsComplexType)){如果(prop.HideSurroundingHtml){<text>@Html.Editor(prop.PropertyName)</text>} 别的 {字符串 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 +=需要";}<div class="editor-container @css"><div class="editor-label">@if (!String.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString())){//使用 LabelWithForThatMatchesTheIdOfTheInput 而不是 Label 因为一个错误(在 MVC 3 中修复)@Html.LabelWithForThatMatchesTheIdOfTheInput(prop.PropertyName)}@if (prop.IsRequired && prop.ModelType.FullName != "System.Boolean"){@Html.Raw(" <span class="required">*<span>");}

<div class="editor-field">@* 这是导致我的问题的行 *@@Html.Editor(prop.PropertyName)@Html.ValidationMessage(prop.PropertyName)

}}//foreach//使用 TemplateHint (UIHint) 循环遍历模型中的所有项目foreach (ViewData.ModelMetadata.Properties.Where(下午 =>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: 检查重复的 js 文件包含@Html.Editor(prop.PropertyName, prop.TemplateHint + "-Script")}}

解决方案

我建议使用 EditorFor 而不是 Editor.

Html.EditorFor(x => x.Title)

代替:

Html.Editor("Title")

通过这种方式,视图不仅可以利用您的视图模型,而且在这种情况下它的行为也符合预期.

ASP.NET MVC 3.0 RTM (Razor) 示例:

型号:

公共类 MyViewModel{公共字符串标题{获取;放;}}

控制器:

公共类 HomeController : 控制器{公共 ActionResult 索引(){ViewBag.Title = "ViewBag 标题";ViewData["Title"] = "ViewData 标题";var 模型 = 新的 MyViewModel{标题 = "模型标题"};返回视图(模型);}}

查看:

@model AppName.Models.MyViewModel@{ViewBag.Title = "首页";}@Html.EditorFor(x => x.Title)@{ViewBag.Title = "其他标题";}

因此,无论我们在这里如何滥用,编辑器模板都会使用正确的模型标题(如果我们使用 Html.Editor("Title"),则情况并非如此).

My Model contains a property named Title, and in my Create view I set the page title using ViewBag.Title.

This creates the following problem: the form generated by Html.Editor will display the text from ViewBag.Title, instead of the model's Title value.

The only workaround I have found is first calling Html.Editor, and then setting the View.Title.

Does anyone have a better solution?

Edit 1: I am using MVC 3.

Edit 2: This is my 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")
    }    

}

解决方案

I would recommend using EditorFor instead of Editor.

Html.EditorFor(x => x.Title)

instead of:

Html.Editor("Title")

This way not only that the view takes advantage of your view model but it behaves as expected in this case.

Example with ASP.NET MVC 3.0 RTM (Razor):

Model:

public class MyViewModel
{
    public string Title { get; set; }
}

Controller:

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);
    }
}

View:

@model AppName.Models.MyViewModel
@{
    ViewBag.Title = "Home Page";
}

@Html.EditorFor(x => x.Title)

@{
    ViewBag.Title = "Some other title";
}

So no matter how much we try to abuse here the editor template uses the correct model title (which is not the case if we used Html.Editor("Title")).

这篇关于我的模型中名为 Title 的属性与视图中的 View.Title 之间的绑定冲突(在 MVC 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆