asp.net mvc - How to handle a null reference exception inside html helpers -


i working on asp.net mvc web application, , have following viewmodel:-

public class serverjoin     {         public tmsserver server { get; set; }         public resource resource { get; set; }         public technology technology { get; set; }         public componentdefinition componentdefinition { get; set; }         public sdorganization site { get; set; }         public sdorganization customer { get; set; }         public ienumerable<networkinfo> networkinfo { get; set; }     } 

on _createandupdate view used edit , create new serverjoin object have following:-

@model tms.viewmodels.serverjoin  <span class="f">ip address</span>       @html.editorfor(model=>model.networkinfo.ipaddress)     @html.validationmessagefor(model=>model.networkinfo.ipaddress) |      @html.checkbox("isipunique",new  { @checked = "checked" } ) ip unique.   </div> 

but problem facing networkinfo collection unable directly access ipaddress property. , in case wrote following foreach:-

@foreach(var n in model.networkinfo) 

i null reference exception

object reference not set instance of objec

so how can solve issue , have following:-

  1. in case inside edit view, should display ipaddress(1 editorfor, 2 editorfor, etc).

  2. in case inside create view, should create 1 ip editorfor ?

thanks

try this

@if(model.networkinfo!=null) {    html.editorfor(model=>model.networkinfo.ipaddress)    html.validationmessagefor(model=>model.networkinfo.ipaddress) |     html.checkbox("isipunique",new  { @checked = "checked" } ) ip unique.  } else {     foreach(var n in model.networkinfo)     {        ......      }  } 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -