-3

Hi I am new to MVC I am trying to disable a button if a few textboxfor fields are not entered or are null. I have tried the following code below but no luck, I get an object null error.

@if (String.IsNullOrEmpty(modelData.Email))
{
    <input type="submit" class="btn btn-default" value="Register" onclick="JavascriptFunction()" disabled="disabled" />
}
else
{
    <input type="submit" class="btn btn-default" value="Register" onclick="JavascriptFunction()" />
}

HTML Text box definition

@Html.TextBoxFor(m => m.Email, new { @placeholder = "example: dlamini@gmail.com", @class = "form-control", id = "message", onkeypress = "capLock(event)" })

This happens when the Registration.cshtml page loads, please assist.

Papi
  • 555
  • 13
  • 40

1 Answers1

1

I think modelData must be null. Depending on your .NET version, you could try either

@if (String.IsNullOrEmpty(modelData?.Email))

or

@if (modelData != null && String.IsNullOrEmpty(modelData.Email))
harriyott
  • 10,505
  • 10
  • 64
  • 103