Listview Model Binding - NullReferenceException

4 posts, 0 answers
  1. Carey
    Carey avatar
    3 posts
    Member since:
    Feb 2012

    Posted 11 Jan Link to this post

    Hi,

    I'm trying to have my ListView bind to a model item, so that when postback occurs, the entries updated to the listview, are reflected in the associated model field.When I attempt this with the below code though, I receive a NullReferenceException from the view code that I've posted below. I've included the model also, as well as the stack trace. Any help with this one is greatly appreciated! Thanks!

     

    Model:

    public class CustomerCreateModel
    {
        #region private fields
        private ICollection<CreateSecurityQuestionModel> _SecurityQuestions = new List<CreateSecurityQuestionModel>();
        #endregion
     
        [Required(AllowEmptyStrings = false), Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Required(AllowEmptyStrings = false), Display(Name = "Last Name")]
        public string LastName { get; set; }
        public ICollection<SecurityQuestionModel> SecurityQuestions
        {
            get { return _SecurityQuestions; }
            set { _SecurityQuestions = value; }
        }
    }

     

    View Code:

    @model CustomerCreateModel
     
    <div class="form-group">
        @Html.Label("Security Questions", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div style="clear: both;">
                <a class="k-button k-button-icontext k-add-button" href="#"><span class="k-icon k-add"></span>Add new security question</a>
                @(Html.Kendo().ListView<SecurityQuestionModel>(Model.SecurityQuestions)
                    .Name("SecurityQuestionsListView")
                    .TagName("div")
                    .ClientTemplateId("security-qa-template")
                    .DataSource(dataSource => dataSource
                        .Model(model => model.Id("Id"))
                        .PageSize(10)
                        .Create(create => create.Action("Create", "Customers"))
                        .Update(update => update.Action("Update", "Customers"))
                        )
                    .Pageable()
                    .Editable(e => e.TemplateName("QuestionAnswerEditor"))
                )
            </div>
        </div>
    </div>
  2. Carey
    Carey avatar
    3 posts
    Member since:
    Feb 2012

    Posted 11 Jan in reply to Carey Link to this post

    Apologies... Exception details are below:

    System.NullReferenceException was unhandled by user code
      HResult=-2147467261
      Message=Object reference not set to an instance of an object.
      Source=App_Web_kkzcd5h3
      StackTrace:
           at ASP._Page_Views_Customers_Create_cshtml.Execute() in C:\SmartSafeMgr\SmartSafeMgr_Web\Views\Customers\Create.cshtml:line 285
           at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
           at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
           at System.Web.WebPages.StartPage.RunPage()
           at System.Web.WebPages.StartPage.ExecutePageHierarchy()
           at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
           at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
           at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
           at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
           at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
           at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
           at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
      InnerException:
  3. Carey
    Carey avatar
    3 posts
    Member since:
    Feb 2012

    Posted 11 Jan in reply to Carey Link to this post

    Apologies... Exception details are below:

    System.NullReferenceException was unhandled by user code
      HResult=-2147467261
      Message=Object reference not set to an instance of an object.
      Source=App_Web_kkzcd5h3
      StackTrace:
           at ASP._Page_Views_Customers_Create_cshtml.Execute() in C:\SmartSafeMgr\SmartSafeMgr_Web\Views\Customers\Create.cshtml:line 285
           at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
           at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
           at System.Web.WebPages.StartPage.RunPage()
           at System.Web.WebPages.StartPage.ExecutePageHierarchy()
           at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
           at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
           at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
           at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
           at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
           at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
           at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
      InnerException:

     

  4. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1727 posts

    Posted 13 Jan Link to this post

    Hi Carey,

    From the stack trace it seems that the error originates in the Create action. You can run the application in debug mode (F5) and Visual Studio would probably show more information and clues to what is causing the error.

    With that said, please make sure that you configure the Read, Create, Update methods as described in the following article and see how the behavior changes.



    Regards,
    Viktor Tachev
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top