asp.net-mvc - 模型中名为 Title 的属性与 View 中名为 View.Title 的属性之间的绑定(bind)冲突(在 MVC 中)

标签 asp.net-mvc razor model conflict

我的模型包含一个名为 Title 的属性,在我的 Create View 中,我使用 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")

这样, View 不仅可以利用您的 View 模型,而且在这种情况下它的行为也符合预期。

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

型号:

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

查看:

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

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

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

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

关于asp.net-mvc - 模型中名为 Title 的属性与 View 中名为 View.Title 的属性之间的绑定(bind)冲突(在 MVC 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4375323/

相关文章:

asp.net-mvc - 如何在 Razor 中设置 html id 属性

javascript - 如何保护从站点窃取的经度/纬度值

ruby-on-rails - Rails 模型测试

javascript - ASP.net Razor MVC5 在我的 cshtml 页面内的 javascript 中插入 @{}

asp.net - 当 StatusCode 不为 200 时,自定义 404 页面不显示

javascript - MVC3 Html.TextBoxFor 内容更改时提交表单

c++ - 从 QAbstractItemModel 子类化时支持拖放

node.js - Sails 中模型的独特属性

asp.net-mvc - 客户端验证未显示消息

c# - 在 ASP.NET MVC 中解析 JSON 值时出错?