繁体   English   中英

ASP.NET MVC:在视图中呈现字符串

[英]ASP.NET MVC : render a string in a view

我有这个代码部分

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

但是当我渲染它时,字符串“是”或“否”不会出现在浏览器中。

如果item.ProductsRequest有一些信息,我想在列中写“是”。

感谢您的帮助

如果您只想写出值,那么只需通过<text>块将字符串放入相应的 if-else 块中:

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

如果你真的想简洁:

<td>
    @(item?.ProductsRequest != null ? "yes" : "no")
</td> 

正如 Rion Williams 刚才所说,您可以使用下面的代码来实现您的目标:

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

但是如果你想使用 TextBox 函数渲染,你可以这样做:

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

我想在栏里写“是”

您不需要使用@Html.TextBox ,只需以这种方式显示文本

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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