微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 我的模型中名为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 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”)则不是这种情况).

原文地址:https://www.jb51.cc/aspnet/248458.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐