This is a migrated thread and some comments may be shown as answers.

Listview Model Binding - NullReferenceException

3 Answers 337 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Carey
Top achievements
Rank 1
Carey asked on 11 Jan 2017, 02:43 PM

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>

3 Answers, 1 is accepted

Sort by
0
Carey
Top achievements
Rank 1
answered on 11 Jan 2017, 02:46 PM

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:
0
Carey
Top achievements
Rank 1
answered on 11 Jan 2017, 02:46 PM

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:

 

0
Viktor Tachev
Telerik team
answered on 13 Jan 2017, 11:42 AM
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.
Tags
ListView
Asked by
Carey
Top achievements
Rank 1
Answers by
Carey
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or