Issue with shape rendering with a CustomViewModel

Topics: Troubleshooting
Jan 18, 2012 at 1:15 AM

Hi,

           I am trying to render a content part in edit mode. In the driver, when i try to use the content part as a parameter for model then part is rendered fine but if i use a custom view model then i am getting this error. following is the stack trace. 

          Any idea what could be going wrong?  I am using orchard 1.3.9.

Regards,

Praggie

Server Error in '/OrchardLocal' Application.

Object reference not set to an instance of an object.

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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 109:
Line 110:            var converter = _converters.GetOrAdd(parameter.ParameterType, CompileConverter);
Line 111:            var argument = converter.Invoke(result);
Line 112:            return argument;
Line 113:        }


Source File: F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAttributeStrategy\ShapeAttributeBindingStrategy.cs Line: 111

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Microsoft.CSharp.RuntimeBinder.ExpressionTreeCallRewriter.GenerateLambda(EXPRCALL pExpr) +38
   Microsoft.CSharp.RuntimeBinder.ExpressionTreeCallRewriter.VisitCALL(EXPRCALL pExpr) +38
   Microsoft.CSharp.RuntimeBinder.Semantics.ExprVisitorBase.Dispatch(EXPR pExpr) +50
   Microsoft.CSharp.RuntimeBinder.Semantics.ExprVisitorBase.Visit(EXPR pExpr) +57
   Microsoft.CSharp.RuntimeBinder.ExpressionTreeCallRewriter.Rewrite(TypeManager typeManager, EXPR pExpr, IEnumerable`1 listOfParameters) +160
   Microsoft.CSharp.RuntimeBinder.RuntimeBinder.CreateExpressionTreeFromResult(IEnumerable`1 parameters, ArgumentObject[] arguments, Scope pScope, EXPR pResult) +127
   Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCore(DynamicMetaObjectBinder payload, IEnumerable`1 parameters, DynamicMetaObject[] args, DynamicMetaObject& deferredBinding) +213
   Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(DynamicMetaObjectBinder payload, IEnumerable`1 parameters, DynamicMetaObject[] args, DynamicMetaObject& deferredBinding) +81
   Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(DynamicMetaObjectBinder action, RuntimeBinder binder, IEnumerable`1 args, IEnumerable`1 arginfos, DynamicMetaObject onBindingError) +764
   Microsoft.CSharp.RuntimeBinder.CSharpConvertBinder.FallbackConvert(DynamicMetaObject target, DynamicMetaObject errorSuggestion) +84
   System.Dynamic.DynamicMetaObject.BindConvert(ConvertBinder binder) +23
   System.Dynamic.ConvertBinder.Bind(DynamicMetaObject target, DynamicMetaObject[] args) +55
   System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel) +233
   System.Runtime.CompilerServices.CallSiteBinder.BindCore(CallSite`1 site, Object[] args) +123
   System.Dynamic.UpdateDelegates.UpdateAndExecute1(CallSite site, T0 arg0) +342
   CallSite.Target(Closure , CallSite , Object ) +144
   lambda_method(Closure , Object ) +57
   CallSite.Target(Closure , CallSite , Func`2 , Object ) +105
   System.Dynamic.UpdateDelegates.UpdateAndExecute2(CallSite site, T0 arg0, T1 arg1) +263
   CallSite.Target(Closure , CallSite , Func`2 , Object ) +165
   Orchard.DisplayManagement.Descriptors.ShapeAttributeStrategy.ShapeAttributeBindingStrategy.BindParameter(DisplayContext displayContext, ParameterInfo parameter, TextWriter output) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAttributeStrategy\ShapeAttributeBindingStrategy.cs:111
   Orchard.DisplayManagement.Descriptors.ShapeAttributeStrategy.<>c__DisplayClass7.<PerformInvoke>b__6(ParameterInfo parameter) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAttributeStrategy\ShapeAttributeBindingStrategy.cs:62
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +85
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +325
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +78
   Orchard.DisplayManagement.Descriptors.ShapeAttributeStrategy.ShapeAttributeBindingStrategy.PerformInvoke(DisplayContext displayContext, MethodInfo methodInfo, Object serviceInstance) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAttributeStrategy\ShapeAttributeBindingStrategy.cs:64
   Orchard.DisplayManagement.Descriptors.ShapeAttributeStrategy.<>c__DisplayClass4.<CreateDelegate>b__3(DisplayContext context) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAttributeStrategy\ShapeAttributeBindingStrategy.cs:55
   Orchard.DisplayManagement.Descriptors.<>c__DisplayClass5.<BoundAs>b__2(DisplayContext displayContext) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAlterationBuilder.cs:55
   Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:176
   Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Execute(DisplayContext context) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:85
   Orchard.DisplayManagement.Implementation.DisplayHelper.ShapeExecute(Object shape) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:71
   Orchard.DisplayManagement.Implementation.DisplayHelper.Invoke(String name, INamedEnumerable`1 parameters) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:38
   Orchard.DisplayManagement.Implementation.DisplayHelperBehavior.InvokeMember(Func`1 proceed, Object target, String name, INamedEnumerable`1 args) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DisplayHelperFactory.cs:27
   ClaySharp.<>c__DisplayClass16.<InvokeMember>b__13() in C:\Users\sebros\My Projects\Clay\src\ClaySharp\ClayBehaviorCollection.cs:29
   ClaySharp.ClayBehaviorCollection.Execute(Func`1 proceed, Func`3 linker) in C:\Users\sebros\My Projects\Clay\src\ClaySharp\ClayBehaviorCollection.cs:13
   ClaySharp.ClayBehaviorCollection.InvokeMember(Func`1 proceed, Object self, String name, INamedEnumerable`1 args) in C:\Users\sebros\My Projects\Clay\src\ClaySharp\ClayBehaviorCollection.cs:29
   Orchard.Core.Shapes.CoreShapes.ContentZone(Object Display, Object Shape, TextWriter Output) in F:\Dev\Orchard.Source\src\Orchard.Web\Core\Shapes\CoreShapes.cs:162

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +0
   System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +72
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +251
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +28
   System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +19
   Orchard.DisplayManagement.Descriptors.ShapeAttributeStrategy.ShapeAttributeBindingStrategy.PerformInvoke(DisplayContext displayContext, MethodInfo methodInfo, Object serviceInstance) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAttributeStrategy\ShapeAttributeBindingStrategy.cs:64
   Orchard.DisplayManagement.Descriptors.ShapeAttributeStrategy.<>c__DisplayClass4.<CreateDelegate>b__3(DisplayContext context) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAttributeStrategy\ShapeAttributeBindingStrategy.cs:55
   Orchard.DisplayManagement.Descriptors.<>c__DisplayClass5.<BoundAs>b__2(DisplayContext displayContext) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAlterationBuilder.cs:55
   Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:176
   Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Execute(DisplayContext context) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:85
   Orchard.DisplayManagement.Implementation.DisplayHelper.ShapeExecute(Object shape) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:71
   Orchard.DisplayManagement.Implementation.DisplayHelper.Invoke(String name, INamedEnumerable`1 parameters) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:38
   Orchard.DisplayManagement.Implementation.DisplayHelperBehavior.InvokeMember(Func`1 proceed, Object target, String name, INamedEnumerable`1 args) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DisplayHelperFactory.cs:27
   ClaySharp.<>c__DisplayClass16.<InvokeMember>b__13() in C:\Users\sebros\My Projects\Clay\src\ClaySharp\ClayBehaviorCollection.cs:29
   ClaySharp.ClayBehaviorCollection.Execute(Func`1 proceed, Func`3 linker) in C:\Users\sebros\My Projects\Clay\src\ClaySharp\ClayBehaviorCollection.cs:13
   ClaySharp.ClayBehaviorCollection.InvokeMember(Func`1 proceed, Object self, String name, INamedEnumerable`1 args) in C:\Users\sebros\My Projects\Clay\src\ClaySharp\ClayBehaviorCollection.cs:29
   ASP._Page_Core_Contents_Views_Content_Edit_cshtml.Execute() in f:\Dev\Orchard.Source\src\Orchard.Web\Core\Contents\Views\Content.Edit.cshtml:5
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +88
   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.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +279
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +113
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model) +32
   Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.Render(ShapeDescriptor shapeDescriptor, DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo, HarvestShapeHit harvestShapeHit) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\ShapeTemplateBindingStrategy.cs:133
   Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.<>c__DisplayClass28.<Discover>b__15(DisplayContext displayContext) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\ShapeTemplateBindingStrategy.cs:117
   Orchard.DisplayManagement.Descriptors.<>c__DisplayClass5.<BoundAs>b__2(DisplayContext displayContext) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Descriptors\ShapeAlterationBuilder.cs:55
   Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:176
   Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Execute(DisplayContext context) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:85
   Orchard.DisplayManagement.Implementation.DisplayHelper.ShapeExecute(Object shape) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:71
   Orchard.DisplayManagement.Implementation.DisplayHelper.Invoke(String name, INamedEnumerable`1 parameters) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:38
   Orchard.DisplayManagement.Implementation.DisplayHelperBehavior.InvokeMember(Func`1 proceed, Object target, String name, INamedEnumerable`1 args) in F:\Dev\Orchard.Source\src\Orchard\DisplayManagement\Implementation\DisplayHelperFactory.cs:27
   ClaySharp.<>c__DisplayClass16.<InvokeMember>b__13() in C:\Users\sebros\My Projects\Clay\src\ClaySharp\ClayBehaviorCollection.cs:29
   ClaySharp.ClayBehaviorCollection.Execute(Func`1 proceed, Func`3 linker) in C:\Users\sebros\My Projects\Clay\src\ClaySharp\ClayBehaviorCollection.cs:13
   ClaySharp.ClayBehaviorCollection.InvokeMember(Func`1 proceed, Object self, String name, INamedEnumerable`1 args) in C:\Users\sebros\My Projects\Clay\src\ClaySharp\ClayBehaviorCollection.cs:29
   ASP._Page_Modules_Aguru_ELearning_Views_QuestionAdmin_Create_cshtml.Execute() in f:\Dev\Orchard.Source\src\Orchard.Web\Modules\Aguru.ELearning\Views\QuestionAdmin\Create.cshtml:5
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +88
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   Orchard.Mvc.ViewEngines.ThemeAwareness.<>c__DisplayClass7.<FindView>b__5(ViewContext viewContext, TextWriter writer, IViewDataContainer viewDataContainer) in F:\Dev\Orchard.Source\src\Orchard\Mvc\ViewEngines\ThemeAwareness\LayoutAwareViewEngine.cs:59
   Orchard.Mvc.ViewEngines.ThemeAwareness.LayoutView.Render(ViewContext viewContext, TextWriter writer) in F:\Dev\Orchard.Source\src\Orchard\Mvc\ViewEngines\ThemeAwareness\LayoutAwareViewEngine.cs:90
   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.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.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.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.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.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.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
   Orchard.Mvc.Routes.HttpAsyncHandler.EndProcessRequest(IAsyncResult result) in F:\Dev\Orchard.Source\src\Orchard\Mvc\Routes\ShellRoute.cs:170
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8971341
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184


 

 

 

Jan 18, 2012 at 1:48 AM

It might help if you showed the actual code from your driver ;)

Jan 18, 2012 at 2:14 AM

Following is the driver class.    Hope this will help in understanding the issue.

 

public class QuestionDriver : ContentPartDriver<QuestionPart>

    {

        private readonly INotifier _notifier;

        private const string TemplateName = "Parts/Question";

 

        public Localizer T { getset; }

 

        public QuestionDriver(INotifier notifier)

        {

            _notifier = notifier;

            T = NullLocalizer.Instance;

        }

 

        protected override DriverResult Display(QuestionPart part, string displayType, dynamic shapeHelper)

        {

            return ContentShape("Parts_Question",

                () => shapeHelper.Parts_Question(ContentItem: part.ContentItem));

        }

 

        protected override DriverResult Editor(QuestionPart part, dynamic shapeHelper) {

        

 

    return ContentShape("Parts_Question_Edit",

                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: BuildQuestionEditorViewmodel(part), Prefix: Prefix));

        }

 

        protected override DriverResult Editor(QuestionPart part, IUpdateModel updater, dynamic shapeHelper) {

            var model = new QuestionViewModel();

 

            if (updater.TryUpdateModel(model, Prefix, nullnull)) {

                //part.Text = model.Text;

                //part.Type = model.SelectedQuestionType;

                //part.Stem = model.Stem;

                

                _notifier.Information(T("Question edited successfully"));

            }

            else

            {

                _notifier.Error(T("Error during Question update!"));

            }

            return Editor(part, shapeHelper);

        }

 

        private QuestionViewModel BuildQuestionEditorViewmodel(QuestionPart part) {

            var model = new QuestionViewModel();

            if (part != null) {

                model.Text = part.Text;

                model.SelectedQuestionType = part.Type;

               // model.QuestionTypes = new SelectList(UtilityExtensions.EnumToList<QuestionType>());

               //if (part.Stem != null)

               //    model.Stem.Text = part.Stem.Text;

            }

           

            return model;

        }

    }

Jan 18, 2012 at 3:06 AM
Edited Jan 18, 2012 at 3:06 AM

Aha, I see exactly the problem. You simply need to write your method like this:

 

   protected override DriverResult Editor(QuestionPart part, dynamic shapeHelper) {
     // Build viewmodel first
     var viewModel = BuildQuestionEditorViewmodel(part);
     // Then make the content shape
    return ContentShape("Parts_Question_Edit",

                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model:viewModel , Prefix: Prefix));

   } 

 

You might think it strange that one works and the other doesn't, and it goes against what you may know about C#! But it's because the shapeHelper.EditorTemplate(...) call isn't a normal method call, it's a dynamically invoked method and when it runs there are various behaviours processing the input parameters, and it's getting a bit confused what to do with that further method invoke in there (possibly because it's a private method). At least ... I'm pretty sure that's the problem. It's a bit hard to explain in any more detail the technical reasons why it would cause an error.

Jan 18, 2012 at 3:22 AM

Thanks for quick response randompete, but above code does not fix the problem.  

In fact I have seen that some of the modules were passing the viewmodel to ContentShape method the way i was doing. You are right, I am not able to understand why this will make a difference.

Jan 18, 2012 at 4:13 AM

Strange then ... you're right, it shouldn't make a different, but odd things can happen with dynamic invokes.

What's in your view, and what does QuestionViewModel.cs look like?

Jan 18, 2012 at 4:39 AM

Can it be because QuestionPartRecord has a reference to another part QuestionStemPartRecord and i am not handling it correctly?

Here are some other classes.

Viewmodel:

    class QuestionViewModel
    {
        public SelectList QuestionTypes { getset; }
 
        public QuestionType SelectedQuestionType { getset; }
 
        public QuestionStemPartRecord Stem { getset; }
 
        public string Text { getset; }
    }

View: 

@{ Layout.Title = T("New Question").ToString(); }
@using (Html.BeginFormAntiForgeryPost())
{
        @Html.ValidationSummary(true)
	    @Display(Model)
}


Editor Template:

@model ELearning.ViewModels.QuestionViewModel
@using Orchard.ContentManagement;
@{ Layout.Title = T("Edit Question").ToString(); }
<fieldset>
    <legend>@T("Question settings")</legend>
    <div>
        @Html.LabelFor(m => m.Text, @T("Text"))
        @Html.EditorFor(m => m.Text)
        @Html.ValidationMessageFor(m => m.Text, "*")
    </div>

</fieldset>

Jan 18, 2012 at 5:41 AM

I think the problem is that QuestionViewModel is private. You need to preface the class with public.

Jan 18, 2012 at 5:52 AM

Thanks a lot randompete. This fixes the error.

I have wasted  almost whole day to fix this but did not even think that a silly mistake could also lead to such issue.

Jan 18, 2012 at 6:22 AM
Edited Jan 18, 2012 at 6:23 AM

It's an easy one to miss; something I've learned to notice quite quickly because I too have wasted countless hours after making similar mistakes! (And it's always the silly little mistakes that waste the most time and are hardest to track down...)

It also goes to show, always post all related code when you have errors, I might have spotted that straight away if it had been in your first post ;)