asp.net mvc - MVC View Model Grab single record not a list of records, rewrite -


i realized last week view model populating list of records when want single record of selected record view. i've deduced view model wrong can't seem wire brain on how change spits out selected record , not list of records database.

public class summaryvm : baseviewmodel {     public ilist<recordvm> records { get; set; }     public summaryvm()     {         this.records = new list<recordvm>();     } } 

i guessing on thinking everything.

edit:

new viewmodel

public class summaryvm : baseviewmodel     {     public recordvm record { get; set; } } 

my controller

    public actionresult summary(int id)     {         var vm = new summaryvm         {             record = new recordvm()         };          return view(vm);     } 

change collection single instance ?

public class summaryvm : baseviewmodel {     public recordvm record { get; set; } } 

now have make sure wherever using view model,your code updated treat single object instead of collection (no foreach loop etc on record property)

note that, not have constructor initializes record property record instance. need explicitly before accessing property on record type

var vm= new summaryvm { record = new recordvm() }; vm.recordid = 123; 

do null on record property before accessing of it's properties prevent "object reference not set instance" error

@model summaryvm <h2>record</h2> @if(model.record!=null) {   <h3>@model.record.recordi</h3> } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -