c# - inserting a string value into viewmodel list -




i posted similar question 2 weeks ago, having issues getting debug successfully. @stephen muecke helping me troubleshoot that. reposting, since not getting traction on second half of problem, outlined below.

working on site function shows list of companies, based on membertype, running issue in how list being built in c#. when test code, unhandled exception error, , debugging indicates list empty.

here viewmodel:

public class memberlistviewmodel {     public list<string> memberlist { get; set; }     public string membertype { get; set; } } 

here controller code:

public class memberlistcontroller : controller { public actionresult memberlist() {     return partialview(preparememberlistviewmodel()); }  private memberlistviewmodel preparememberlistviewmodel() {     memberlistviewmodel viewmodel = new memberlistviewmodel();      string orgtype = "distributor"; //todo: hardcoded dev      datatable table = new datatable();      using (var connection = new sqlconnection("provider=sqloledb;data source=db.site.net;persist security info=true;initial catalog=db;user id=sa;password=pw"))     {         connection.open(); if (orgtype == "manufacturer")         {             using (var command = new sqlcommand("select member dbo.view [member type] = 'manufacturer' , [member code] <> 'associate - hbw'", connection))                 table.load(command.executereader());         }         else if (orgtype == "hbw")         {             using (var command = new sqlcommand("select member dbo.view [member type] = 'manufacturer' , [member code] = 'associate - hbw'", connection))                 table.load(command.executereader());         }         else         {             using (var command = new sqlcommand("select member dbo.view [member type] = '" + orgtype + "'", connection))                 table.load(command.executereader());         } connection.close();     }      (int = 0; < table.rows.count; i++)         {             string membername = table.rows[i]["member"].tostring();             viewmodel.memberlist.add(membername);         }      return viewmodel; } } 

when debug, runs smoothly until

viewmodel.memberlist.add(membername);  

at line, local value viewmodel.memberlist stubbornly stays @ null. have confirmed table has values - in case, 35 rows, company names.

i know missing simple, noob, not sure is. in advance!

you calling add method on null, hence getting typical null reference exception.

make sure initialize empty collection before calling method on /accessing it.

var viewmodel = new memberlistviewmodel(); viewmodel.memberlist= new list<string>(); 

or using object initializer syntax

var viewmodel = new memberlistviewmodel                     {                         memberlist= new list<string>()                     }; 

imho, collection type properties should never null, if no data, should empty list. way fix problem updating class definition have constructor initialize property empty list.

public class memberlistviewmodel {     public list<string> memberlist { get; set; }     public string membertype { get; set; }      public memberlistviewmodel()     {        this.memberlist = new list<string>();     } } 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -