【发布时间】:2014-10-31 18:12:25
【问题描述】:
请帮忙!这让我发疯了。
我正在尝试重构一些代码,因为在模型、视图和控制器中有一堆剪切和粘贴。
查看模型
namespace Foo.Models.Bar
{
[KnownType(typeof(RecruitEditModel))]
public class RecruitEditModel
{
//...
public CommonServicesEditModel Services { get; set; }
Services 是一个属性,它包含模型中的另一个类,我将在其中放置公共代码。
在 Foo\Views 中查看
@model RecruitEditModel
@* ... *@
@using (Html.BeginForm("RecruitEdit", "AppMgmt", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@* ... *@
@*@Html.Partial("TradeEdit", Model.Services) <<< DOESN'T BIND *@
@Html.EditorFor(m => m.Services) @* BINDS *@
<input type="submit" value="Save" />
}
在嵌套类上使用分部不起作用,因为嵌套类不会绑定。 (http://lostechies.com/jimmybogard/2011/09/07/building-forms-for-deep-view-model-graphs-in-asp-net-mvc/)
所以我必须使用编辑器模板。
Foo\Views\Shared\EditorTemplates\TradeEdit.cshtml 中的编辑器模板
@model Services.CommonServicesEditModel
<div class="left">
<h3>Howdy</h3>
<div class="left theTrade">General Contracting: </div>
@Html.EditorFor(model => model.TradeGC)
@Html.ValidationMessageFor(model => model.TradeGC)
</div>
@*...*@
我遇到的问题是,显然没有找到编辑器模板,而是生成了默认编辑器。
我在尝试调整 CSS 时发现了这一点。未生成 EditorTemplate 中标题中的“您好”。无论我做什么,除了注释掉 EditFor 调用之外,显示的字段都没有变化。
我正在本地 IIS 上运行调试,并尝试重新启动应用程序池并刷新网站,但没有任何乐趣。
如何让编辑器模板中的更改传播到视图?
【问题讨论】:
-
您这里确实有些东西搞砸了...您的上下文没有意义... TradeGC 是什么类型?你的帖子说这个模板叫做 TradeEdit.cshtml,是不是被调用了?我在那里看不到 TradeEdit 类型。您的类型与您显示的完全不同...我在这里也看不到任何嵌套类...服务不是嵌套类,它只是一个属性。嵌套类是在其他类中DEFINED 的类...即
class foo { class bar { ... } }
标签: asp.net-mvc razor