【发布时间】:2012-03-06 08:40:30
【问题描述】:
我已经使用 Umbraco 版本 5 进行了电子安装,并且正在使用 Surface Controller 制作表单。我在这样的视图中添加了表单:
@inherits RenderViewPage
@using System.Web.Mvc.Html;
@using Umbraco.Cms.Web;
@using Umbraco.Cms.Web.Editors;
@{
Layout = "Page.cshtml";
}
<section>
@using (Html.BeginUmbracoForm("SendContactInfo", "ContactSurface"))
{
var contactForm = new ContactModel();
@Html.ValidationSummary(prefix: "contactForm")
@Html.EditorFor(x => contactForm)
<input type="submit" value="Send" />
}
</section>
和控制器:
public class ContactSurfaceController:SurfaceController
{
[HttpPost]
public ActionResult SendContactInfo(
[Bind(Prefix = "contactForm")]
ContactModel model)
{
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
return new JsonResult() { Data = new { Name = "Sucess!"} };
}
}
和型号:
public class ContactModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
[Required]
public string Email { get; set; }
public string Message { get; set; }
}
我在@Html.EditorFor(x => contactForm) 处得到一个空引用异常。这是堆栈恍惚:
[NullReferenceException: Object reference not set to an instance of an object.]
Umbraco.Framework.Localization.Web.Mvc.LocalizingModelMetadataProvider.GetConventionKeyNames(Type containerType, String propertyName, Boolean testSimplePropertyName) +285
Umbraco.Framework.Localization.Web.Mvc.LocalizingModelMetadataProvider.CreateMetadata(IEnumerable`1 attributes, Type containerType, Func`1 modelAccessor, Type modelType, String propertyName) +597
Umbraco.Cms.Web.Mvc.Metadata.UmbracoModelMetadataProvider.CreateMetadata(IEnumerable`1 attributes, Type containerType, Func`1 modelAccessor, Type modelType, String propertyName) +62
System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForProperty(Func`1 modelAccessor, Type containerType, PropertyDescriptor propertyDescriptor) +93
System.Web.Mvc.<GetMetadataForPropertiesImpl>d__2.MoveNext() +226
System.Linq.Buffer`1..ctor(IEnumerable`1 source) +217
System.Linq.<GetEnumerator>d__0.MoveNext() +96
System.Linq.WhereEnumerableIterator`1.MoveNext() +87
System.Web.Mvc.Html.DefaultEditorTemplates.ObjectTemplate(HtmlHelper html, TemplateHelperDelegate templateHelper) +368
System.Web.Mvc.Html.DefaultEditorTemplates.ObjectTemplate(HtmlHelper html) +47
System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions) +714
System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) +1117
System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) +66
System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) +117
System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) +100
System.Web.Mvc.Html.EditorExtensions.EditorFor(HtmlHelper`1 html, Expression`1 expression) +56
ASP._Page_Views_Umbraco_contact_cshtml.Execute() in e:\3fa4128e3b304b75b56af47f1ff5219e\Umbraco5-Smallsite\Views\Umbraco\contact.cshtml:17
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
System.Web.WebPages.StartPage.RunPage() +19
System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
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.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() +8969117
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
我已经为这个错误苦苦挣扎了 4-5 个小时,在谷歌上搜索并尝试了不同的东西。如果有人知道如何解决这个问题,我将不胜感激。
更新 我已经发布了视图和控制器的完整代码以获取更多信息。
Update-2 添加模型以获取更多信息。
Update-3 最后我使用了下面两篇文章中建议的方法。我在使用这种方法时遇到的问题是我直接在 Umbraco 的网站中添加了 SurfaceController,而不是制作另一个项目并在 Umbraco 的网站中引用它。当我创建单独的项目时 - 控制器出现了,一切都很好。我正在将让我想到该想法的帖子标记为正确答案。
我无法解决 Umbraco 本地化思想中的异常问题。
您可以在GitHub repository 找到有关此线程的源代码。
【问题讨论】:
标签: asp.net-mvc-3 razor umbraco