0

I have this code part from a view

<td>
  @if (item.ProductsRequest != null)
  {
      Html.TextBox("yes");
  }
  else
  {
      Html.TextBox("no");
  }
</td>

But when I render it the string "yes" or "no" don't show up in the browser.

I want to write "yes" in the column if there is some information on the item.ProductsRequest.

Thank you for your help

3 Answers 3

2

If you just want to write the value out, then simply put the string in the respective if-else block via a <text> block:

<td>
  @if (item.ProductsRequest != null)
  {
      <text>yes</text>
  }
  else
  {
      <text>no<text>
</td>

If you really want to be succinct:

<td>
    @(item?.ProductsRequest != null ? "yes" : "no")
</td> 
Sign up to request clarification or add additional context in comments.

Comments

1

as Rion Williams just said, you can use this code below to accomplish your goal:

<td>
  @if (item.ProductsRequest != null)
  {
      <text>yes</text>
  }
  else
  {
      <text>no<text>
</td>

But if you want to render using the TextBox function, you can do it this way:

  @if (item.ProductsRequest != null)
  {
      @Html.TextBox("myTextBox", "yes")
  }
  else
  {
      @Html.TextBox("myTextBox", "no")  
  }

Comments

1

I want to write "yes" in the column

You don't need to use @Html.TextBox, just display text in this way

<td>
  @if (item.ProductsRequest != null)
  {
      <span>yes</span>;
  }
  else
  {
      <span>no</span>;
  }
</td>

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.