Home > database >  How to bind the value to input field to a control by using ternary operator check
How to bind the value to input field to a control by using ternary operator check

Time:11-01

I have some input controls where I am trying to bind the value by checking for null which doesn't worked

<input id="LastKnownLatitudeDegree" name="VesselMissing.LastKnownLatitudeDegree" value="@Model.VesselMissing != null ? @Model.VesselMissing.LastKnownLatitudeDegree : ''" class="form-control" max="89" min="0" step="1" type="number" data-dec="0"><span>&#176;</span>

If I am using a null check on top of the control it is not getting visible to the user to enter the data

@if (@Model.VesselMissing != null)
{
   <input id="LastKnownLatitudeDegree" name="VesselMissing.LastKnownLatitudeDegree" value="@Model.VesselMissing.LastKnownLatitudeDegree" class="form-control" max="89" min="0" step="1" type="number" data-dec="0"><span>&#176;</span>
}

I have some bunch of controls like this where I need to bind the value field. There is an other method which I tried out is working but I would like to know if there is a possibility to do as per the first statement

This works but I have some 20 controls so I am considering to make it work as per the first statemet

@{
     string LastKnownLatitudeDegree = string.Empty;
     if(Model.VesselMissing !=null)
     {
         LastKnownLatitudeDegree = Model.VesselMissing.LastKnownLatitudeDegree;
     }
 }
     <input id="LastKnownLatitudeDegree" name="VesselMissing.LastKnownLatitudeDegree" value="@LastKnownLatitudeDegree  hljs-string">" max="89" min="0" step="1" type="number" data-dec="0"><span>&#176;</span>

CodePudding user response:

In both cases you have syntax error.

<input id="LastKnownLatitudeDegree" name="VesselMissing.LastKnownLatitudeDegree"
    value="@(Model.VesselMissing != null ? Model.VesselMissing.LastKnownLatitudeDegree : "")" /> 

or

@if (Model.VesselMissing != null)
{
   <input id="LastKnownLatitudeDegree" name="VesselMissing.LastKnownLatitudeDegree" value="@Model.VesselMissing.LastKnownLatitudeDegree" class="form-control" max="89" min="0" step="1" type="number" data-dec="0"><span>&#176;</span>
}
  • Related