C# ASP.NET MVC3中的Razor和接口继承:为什么可以';难道找不到这个地方吗?

C# ASP.NET MVC3中的Razor和接口继承:为什么可以';难道找不到这个地方吗?,c#,asp.net-mvc-3,razor,C#,Asp.net Mvc 3,Razor,在ASP.NET MVC3应用程序中,我的一个Razor视图有一个奇怪的问题。我收到一个错误,告诉我找不到属性,当我将其值写入调试器控制台时,该属性似乎确实存在 我的视图以一个名为FormEditViewModel的类作为其模型。FormEditViewModel具有类型为IForm的属性,该接口继承自另一个接口IFormObject。IFormObject定义了一个属性名,所以任何实现IForm的东西都必须实现一个名为Name的属性。具体类型表单实现接口IForm,并根据需要定义Name属性

在ASP.NET MVC3应用程序中,我的一个Razor视图有一个奇怪的问题。我收到一个错误,告诉我找不到属性,当我将其值写入调试器控制台时,该属性似乎确实存在

我的视图以一个名为FormEditViewModel的类作为其模型。FormEditViewModel具有类型为IForm的属性,该接口继承自另一个接口IFormObject。IFormObject定义了一个属性名,所以任何实现IForm的东西都必须实现一个名为Name的属性。具体类型表单实现接口IForm,并根据需要定义Name属性

当我运行代码并检查传递给视图的FormEditViewModel对象时,我可以看到它有一个Form类型的属性Form,而这个Form对象有一个Name属性。如果在控制器中插入以下行,以便在传递给视图之前写出FormEditViewModel.Name的值,则输出窗口将显示正确的名称:

Debug.WriteLine("Name: " + vm.Form.Name);
然而,当我运行视图时,我得到一个错误,说“找不到属性MyCompany.MyApplication.Domain.Forms.IForm.Name”。为什么Razor找不到Name属性,而控制器中的C#代码显然可以找到

我的观点是这样的。引发异常的行是@Html.LabelFor(model=>model.Form.Name,“表单标题”)

Form对象不重新定义Name属性,而是从FormObject类继承它。FormObject的启动方式如下:

public class Form : FormObject, IForm
public abstract class FormObject : IFormObject
这是IForm的界面。如您所见,它不声明名称成员,但希望从IFormObject继承名称成员:

using System;
namespace MyCompany.MyApplication.Domain.Forms
{
    public interface IForm : IFormObject
    {
        bool ContainsRequiredFields();
        MyCompany.MyApplication.Domain.Forms.Factories.IFormFieldFactory FormFieldFactory { get; }
        MyCompany.MyApplication.Domain.Forms.Factories.IFormPageFactory FormPageFactory { get; }
        string FriendlyName { get; set; }
        System.Collections.Generic.List<IFormField> GetAllFields();
        System.Collections.Generic.IEnumerable<DomainObjectPlaceholder> GetObjectPlaceholders();
        System.Collections.Generic.IEnumerable<IFormField> GetRequiredFields();
        System.Collections.Generic.IEnumerable<MyCompany.MyApplication.Models.Forms.FormObjectPlaceholder> GetRequiredObjectPlaceholders();
        System.Collections.Generic.List<IFormSection> GetSectionsWithMultipliableOption();
        MyCompany.MyApplication.BLL.IHighLevelFormUtilities HighLevelFormUtilities { get; }
        int? MasterId { get; set; }
        DomainObjectPlaceholder MasterObjectPlaceholder { get; set; }
        MyCompany.MyApplication.Domain.Forms.Adapters.IObjectPlaceholderAdapter ObjectPlaceholderAdapter { get; }
        MyCompany.MyApplication.Domain.Forms.Adapters.IObjectPlaceholderRelationshipAdapter ObjectPlaceholderRelationshipAdapter { get; }
        System.Collections.Generic.List<IFormPage> Pages { get; set; }
        MyCompany.MyApplication.Repository.IAppRepository AppRepo { get; set; }
        int ZooFormId { get; }
        MyCompany.MyApplication.BLL.IPocoUtils PocoUtils { get; }
        void RemoveSectionWithoutChangingDatabase(int sectionId);
        int? TopicId { get; set; }
        DomainObjectPlaceholder TopicObjectPlaceholder { get; set; }
        System.Collections.Generic.IEnumerable<FluentValidation.Results.ValidationResult> ValidationResults { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyCompany.MyApplication.Domain.Forms
{
    public interface IFormObject
    {
        string Name { get; }
        string LongName { get; }
        Guid UniqueId { get; }
        string Prefix { get; }
        string IdPath { get; set; }
        string IdPathWithPrefix { get; }
    }
}
问题是,为什么Razor视图在我运行它时会给我以下异常,因为我希望IForm从IFormObject继承它的Name属性

Server Error in '/' Application.

The property MyCompany.MyApplication.Domain.Forms.IForm.Name could not be found.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: The property MyCompany.MyApplication.Domain.Forms.IForm.Name could not be found.

Source Error: 


Line 24:                 </div>
Line 25:                 <div class="formFieldContainer">
Line 26:                     @Html.LabelFor(model => model.Form.Name, "Form title")
Line 27:                     @Html.EditorFor(model => model.Form.Name)
Line 28:                     @Html.ValidationMessageFor(model => model.Form.Name)

Source File: c:\Users\me\Documents\Visual Studio 2010\Projects\zooDBMain\zooDB\zooDB\Views\ZooForm\Edit.cshtml    Line: 26 

Stack Trace: 


[ArgumentException: The property MyCompany.MyApplication.Domain.Forms.IForm.Name could not be found.]
   System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForProperty(Func`1 modelAccessor, Type containerType, String propertyName) +505385
   System.Web.Mvc.ModelMetadata.GetMetadataFromProvider(Func`1 modelAccessor, Type modelType, String propertyName, Type containerType) +101
   System.Web.Mvc.ModelMetadata.FromLambdaExpression(Expression`1 expression, ViewDataDictionary`1 viewData) +421
   System.Web.Mvc.Html.LabelExtensions.LabelFor(HtmlHelper`1 html, Expression`1 expression, String labelText) +56
   ASP._Page_Views_ZooForm_Edit_cshtml.Execute() in c:\Users\me\Documents\Visual Studio 2010\Projects\zooDBMain\zooDB\zooDB\Views\ZooForm\Edit.cshtml:26
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +272
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
   System.Web.WebPages.StartPage.RunPage() +58
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +94
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +173
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8971485
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.547
“/”应用程序中出现服务器错误。 找不到属性MyCompany.MyApplication.Domain.Forms.IForm.Name。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:System.ArgumentException:找不到属性MyCompany.MyApplication.Domain.Forms.IForm.Name。 源错误: 第24行: 第25行: 第26行:@Html.LabelFor(model=>model.Form.Name,“表单标题”) 第27行:@Html.EditorFor(model=>model.Form.Name) 第28行:@Html.ValidationMessageFor(model=>model.Form.Name) 源文件:c:\Users\me\Documents\Visual Studio 2010\Projects\zoodMain\zooDB\zooDB\Views\ZooForm\Edit.cshtml行:26 堆栈跟踪: [ArgumentException:找不到MyCompany.MyApplication.Domain.Forms.IForm.Name属性。] System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForProperty(Func`1 modelAccessor,类型containerType,字符串propertyName)+505385 System.Web.Mvc.ModelMetadata.GetMetadataFromProvider(Func`1 modelAccessor,类型modelType,字符串propertyName,类型containerType)+101 System.Web.Mvc.modelmetada.FromLambdaExpression(表达式'1 Expression,ViewDataDictionary'1 viewData)+421 System.Web.Mvc.Html.LabelExtensions.LabelFor(HtmlHelper`1html,表达式`1表达式,字符串labelText)+56 ASP.\u Page\u Views\u ZooForm\u Edit\u cshtml.Execute()在c:\Users\me\Documents\Visual Studio 2010\Projects\zoodMain\zooDB\zooDB\Views\ZooForm\Edit.cshtml:26中 System.Web.WebPages.WebPageBase.ExecutePageHierarchy()+272 System.Web.Mvc.WebViewPage.ExecutePageHierarchy()+81 System.Web.WebPages.StartPage.RunPage()+58 System.Web.WebPages.StartPage.ExecutePageHierarchy()+94 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext-pageContext、TextWriter-writer、WebPageRenderingBase-startPage)+173 System.Web.Mvc.RazorView.RenderView(ViewContext、TextWriter、Object实例)+220 System.Web.Mvc.buildmanager compiledview.Render(ViewContext、TextWriter)+115 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext上下文)+303 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext ControllerContext,ActionResult ActionResult)+13 System.Web.Mvc.c__显示Class1C.b__19()+23 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器,ResultExecutingContext预文本,Func`1 continuation)+260 System.Web.Mvc.c__DisplayClass1e.b__1b()+19 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext ControllerContext,IList`1过滤器,ActionResult ActionResult)+177 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)+343 System.Web.Mvc.Controller.ExecuteCore()+116 System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)+97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext RequestContext)+10 System.Web.Mvc.c__显示类b.b__5()+37 System.Web.Mvc.Async.c__显示类1.b__0()+21 System.Web.Mvc.Async.c_uuudisplayClass8`1.b_uuu7(IAsyncResult)+12 System.Web.Mvc.Async.WrappedAsyncResult`1.End()+62 System.Web.Mvc.c_uuudisplayClasse.b_uuud()+50 System.Web.Mvc.SecurityUtil.b___0(操作f)+7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(操作)+22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+60 System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.EndProcessRequest(IAsyncResult结果)+9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+8971485 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+184 版本信息:Microsoft.NET Framework版本:4.0.30319;ASP.NET版本:4.0.30319.547 我理解我的继承层次结构有点混乱,这是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyCompany.MyApplication.Domain.Forms
{
    public interface IFormObject
    {
        string Name { get; }
        string LongName { get; }
        Guid UniqueId { get; }
        string Prefix { get; }
        string IdPath { get; set; }
        string IdPathWithPrefix { get; }
    }
}
Server Error in '/' Application.

The property MyCompany.MyApplication.Domain.Forms.IForm.Name could not be found.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: The property MyCompany.MyApplication.Domain.Forms.IForm.Name could not be found.

Source Error: 


Line 24:                 </div>
Line 25:                 <div class="formFieldContainer">
Line 26:                     @Html.LabelFor(model => model.Form.Name, "Form title")
Line 27:                     @Html.EditorFor(model => model.Form.Name)
Line 28:                     @Html.ValidationMessageFor(model => model.Form.Name)

Source File: c:\Users\me\Documents\Visual Studio 2010\Projects\zooDBMain\zooDB\zooDB\Views\ZooForm\Edit.cshtml    Line: 26 

Stack Trace: 


[ArgumentException: The property MyCompany.MyApplication.Domain.Forms.IForm.Name could not be found.]
   System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForProperty(Func`1 modelAccessor, Type containerType, String propertyName) +505385
   System.Web.Mvc.ModelMetadata.GetMetadataFromProvider(Func`1 modelAccessor, Type modelType, String propertyName, Type containerType) +101
   System.Web.Mvc.ModelMetadata.FromLambdaExpression(Expression`1 expression, ViewDataDictionary`1 viewData) +421
   System.Web.Mvc.Html.LabelExtensions.LabelFor(HtmlHelper`1 html, Expression`1 expression, String labelText) +56
   ASP._Page_Views_ZooForm_Edit_cshtml.Execute() in c:\Users\me\Documents\Visual Studio 2010\Projects\zooDBMain\zooDB\zooDB\Views\ZooForm\Edit.cshtml:26
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +272
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
   System.Web.WebPages.StartPage.RunPage() +58
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +94
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +173
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8971485
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.547
<%: Html.HiddenFor(m => (m as IFormObject).Name) %>