1
<div class="form-gridcontrol">
    <label>Notes</label>
    @Html.CustomTextArea(m => m.Notes)
</div>

In ASP.NET MVC , I have created a custom textarea and inputing/displaying data from the database using a Model.Above is the code where you can see the Notes are getting assigned to @Html.CustomTextArea. I have a situation where , I need to display a text "Not Applicable" if there is no value in "m.Notes" How I should right the logic in the above code? Please guide.

2 Answers 2

1

There are multiple possible ways for this. One of the way is that you can populate in the controller action from where it is loaded like:

public ActionResult YourActionMethod()
{
     ............
     ............
     if(String.IsNullOrEmpty(model.Notes))
          model.Notes = "Not Applicable";

     return View(model);
}

Another way can be to introdcue the backing field on your property and write in it's getter:

private String _notes;
public String Notes
{
   get
   {
      return String.IsNullOrEmpty(_notes) ? "Not Applicable" : _notes;
   }
   set
   {
      _notes = value;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

@if (Model.Notes != null)
  {
      @Html.CustomTextArea(m => m.Notes)
  }
else
  {
    @Html.CustomTextArea( m => m.Notes, new { @Value = "Not Applicable"})
  }

edit:this is not working with textarea

else
{
  @Html.CustomTextArea(m => m.Notes, new {id="mytextarea"})
  <script>
     $("#mytextarea").text("Not Applicable")
  </script>
}

I got a trick for you :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.