简体   繁体   中英

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

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> 

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM