绑定在我的模型和View.Title一个名为Title属性之间的矛盾,我认为(在MVC) [英] Binding conflict between a property named Title in my Model and View.Title in my View (in MVC)

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

问题描述

我的模型包含名为属性标题,并在我的创建观点我设定使用页面的标题 ViewBag.Title

这将创建下列问题:通过 Html.Editor 生成的表单将显示来自 ViewBag.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){
    <跨度类=主编对象简单的> @ ViewData.ModelMetadata.SimpleDisplayText< / SPAN>
}其他{
    在ViewData.ModelMetadata.Properties.Where的foreach(VAR道具(
            PM =>
                pm.ShowForEdit
                &功放;&安培; !ViewData.TemplateInfo.Visited(下午)
                &功放;&安培; pm.ModelType!= typeof运算(System.Data.EntityState)
                &功放;&安培; !pm.IsComplexType
            )
        )
        {
        如果(prop.HideSurroundingHtml){
            <文字和GT; @ Html.Editor(prop.PropertyName)LT; /文字和GT;
        }其他{
            字符串的CSS =;
            如果(prop.Model = NULL&放大器;!&安培;!prop.Model.GetType()= NULL)
            {
                CSS + =+ prop.Model.GetType()的ToString()ToLower将()更换(' - ''。')。;
            }
            如果(prop.DataTypeName!= NULL)
            {
                CSS + =+ prop.DataTypeName.ToLower();
            }
            如果(prop.IsRequired&安培;&安培;!prop.ModelType.FullName =System.Boolean)
            {
                CSS + =需要;
            }            < D​​IV CLASS =主编容器@css>
                 < D​​IV CLASS =编辑标记>
                    @if(!String.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString()))
                    {
                        //使用LabelWithForThatMatchesTheIdOfTheInput标签,而不是因为错误(固定MVC 3)
                       @ Html.LabelWithForThatMatchesTheIdOfTheInput(prop.PropertyName)
                    }
                    @if(prop.IsRequired&安培;&安培;!prop.ModelType.FullName =System.Boolean)
                    {
                        @ Html.Raw(<跨度类= \\要求\\> * LT;跨度>);
                    }
                < / DIV>
                < D​​IV CLASS =主编场>
                    @ *这导致我的问题*行@
                    @ Html.Editor(prop.PropertyName)
                    @ Html.ValidationMessage(prop.PropertyName)
                < / DIV>
            < / DIV>
        }
        } //的foreach    //循环虽然模型中的所有物品与TemplateHint(UIHint)
    在ViewData.ModelMetadata.Properties.Where的foreach(VAR道具(
           PM => pm.ShowForEdit
           &功放;&安培; !ViewData.TemplateInfo.Visited(下午)
           &功放;&安培; 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 +-Scr​​ipt)
    }}


解决方案

我会建议使用 EditorFor 而不是编辑

  Html.EditorFor(X => x.Title)

而不是:

  Html.Editor(标题)

此方式不仅认为需要你的视图模型的优势,但它的行为与预期在这种情况下。

在ASP.NET MVC 3.0 RTM(剃刀)例如:

型号:

 公共类MyViewModel
{
    公共字符串名称{搞定;组; }
}

控制器:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        ViewBag.Title =ViewBag称号;
        计算机[标题] =ViewData的称号;
        VAR模型=新MyViewModel
        {
            标题=模范称号
        };
        返回查看(模型);
    }
}

查看:

  @model AppName.Models.MyViewModel
@ {
    ViewBag.Title =主页;
}@ Html.EditorFor(X => x.Title)@ {
    ViewBag.Title =一些其他的称号;
}

所以,无论我们多么努力在这里乱编辑模板使用正确的车型名称(这是不是如果我们使用的情况下 Html.Editor(标题))。

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")).

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆