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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 我的模型中名为Title的属性与我视图中的View.Title之间的绑定冲突(在MVC中)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的模型包含一个名为Title的属性,在我的Create视图中,我使用ViewBag.Title设置页面标题.

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

我找到的唯一解决方法是首先调用Html.Editor,然后设置View.Title.@H_403_3@

有没有人有更好的解决方案?@H_403_3@

编辑1:我正在使用MVC 3.@H_403_3@

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

@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)

代替:@H_403_3@

Html.Editor("Title")

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

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

模型:@H_403_3@

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

控制器:@H_403_3@

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

视图:@H_403_3@

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

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

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

所以无论我们在这里滥用多少,编辑器模板都使用正确的模型标题(如果我们使用Html.Editor(“Title”)则不是这种情况).@H_403_3@

原文链接:https://www.f2er.com/aspnet/248458.html

猜你在找的asp.Net相关文章