3

I am new in angularjs. i am using angularjs in .Net MVC razor syntax but when i load my page its gives error that is Null reference exception. Below is my Code.

@Html.TextBoxFor(model => Model.PlantCode, "PlantCode", 
    new { @class = "form-control", ng_model = "PlantCode", 
        required = "required", ng_pattern = "/^[a-zA-Z0-9_-]+$/", 
        oninvalid = "this.setCustomValidity(' ')",
        data_ng_init = string.Format("PlantCode = '{0}'", Model.PlantCode.Replace("'", @"\'")) }) 

When i load page its gives Null reference exception that is Model.PlantCode is not set to an instance of an object. Please help me in finding solution. Thanks

Chetan
  • 6,711
  • 3
  • 22
  • 32
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Izzy May 02 '17 at 09:40
  • I think your Model.PlantCode is null in your case. You should check this in your controller. you can set to string.Empty if you want. While your PlantCode property is null, the Replace method will continue to throw the exception. – Coskun Ozogul May 02 '17 at 10:05
  • my question is different. the above code throws error on page load only when i use data-ng-init with textbox. Otherwise its working fine. – Naveed Ahmed May 02 '17 at 10:06
  • @Coskun Ozogul yes sir you are right but its working fine without data-ng-init even if Model.PlantCode is null. i need data-ng-init because when i load this view with Model values it does not display that values in textbox without data-ng-init – Naveed Ahmed May 02 '17 at 10:08
  • Can you post your code without data-ng-init please ? – Coskun Ozogul May 02 '17 at 10:08
  • The below code is without data-ng-init. @Html.TextBoxFor(model => Model.PlantCode, "PlantCode", new { @class = "form-control", ng_model = "PlantCode", required = "required", ng_pattern = "/^[a-zA-Z0-9_-]+$/", oninvalid = "this.setCustomValidity(' ')"}) – Naveed Ahmed May 02 '17 at 10:12
  • Did you debug and check if Model.PlantCode is null or not ? – Chetan May 02 '17 at 10:36
  • @Chetan Ranpariya yes i have debug my code and its null but as i have already mentioned on above comments that its working fine without data-ng-init even Model.plantCode is null. Please read above comments – Naveed Ahmed May 02 '17 at 10:41
  • If it is null then surely you will get `NullReferenceException`. What is do you want to set to `data_ng_init` if `Model.PlantCode` is null? You might want to check for null of `Model.PlantCode` before you use Replace method on it. Or you need to make sure that `Model.PlantCode` does have some value in your server code. – Chetan May 02 '17 at 10:44
  • @Chetan Ranpariya Thanks I want to check for null of Model.PlantCode by using "if(Model.Code == null)" but it also throw null exception how can i check? – Naveed Ahmed May 02 '17 at 11:22
  • 1
    Check if the Model itself is null? Where did you put that check/ – Chetan May 02 '17 at 11:23
  • @Chetan Ranpariya Thanks for your suggestion and its working. – Naveed Ahmed May 02 '17 at 11:33

0 Answers0