Stefan
Stefan

Reputation: 585

ASP.NET MVC : render a string in a view

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

Upvotes: 0

Views: 43

Answers (3)

Nguyễn Văn Phong
Nguyễn Văn Phong

Reputation: 14228

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>

Upvotes: 1

N&#237;colas
N&#237;colas

Reputation: 416

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")  
  }

Upvotes: 1

Rion Williams
Rion Williams

Reputation: 76577

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> 

Upvotes: 2

Related Questions