Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在asp.net-core-mvc上使用Razor在HTML上删除选项中选择的属性_Asp.net Mvc_Razor_Asp.net Core Mvc - Fatal编程技术网

Asp.net mvc 在asp.net-core-mvc上使用Razor在HTML上删除选项中选择的属性

Asp.net mvc 在asp.net-core-mvc上使用Razor在HTML上删除选项中选择的属性,asp.net-mvc,razor,asp.net-core-mvc,Asp.net Mvc,Razor,Asp.net Core Mvc,我试图将所选属性添加到select中的一个选项中,类似于:项,但在一个奇怪的事件中,文本所选在呈现页面时被删除 例如,我有以下代码: <select class="form-control" name="searchCode"> <option value="0"> Nothing Selected</option> @foreach (string str in results) { string sel = ""; if

我试图将所选属性添加到select中的一个选项中,类似于:
,但在一个奇怪的事件中,文本
所选
在呈现页面时被删除

例如,我有以下代码:

<select 
  class="form-control"
  name="searchCode">
  <option value="0"> Nothing Selected</option>
  @foreach (string str in results) {
    string sel = "";
    if (str.Equals(searchCode)) {
      sel = "selected";
    }
    <option 
      value="@str"
      @sel>
      @str
    </option>
  }
</select>
但它却打印:

<select>
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
</select>

A.
B
C
如果我将代码更改为:

<select 
  class="form-control"
  name="searchCode">
  <option value="0"> Nothing Selected</option>
  @foreach (string str in results) {
    string sel = "";
    if (str.Equals(searchCode)) {
      sel = "selected";
    }
    <option 
      value="@str"
      @sel>
      @str - @sel
    </option>
  }
</select>

未选择任何内容
@foreach(结果中的字符串str){
字符串sel=“”;
if(str.Equals(搜索码)){
sel=“已选定”;
}
@str-@sel
}
我会得到这样的东西:

<select>
  <option value="a">a - </option>
  <option value="b">b - selected</option>
  <option value="c">c - </option>
</select>

a-
b-选定
c-
因此,选定的属性已正确计算,但未按应有的方式打印

对正在发生的事情有什么想法吗?

这似乎是一个bug:

我所做的是将
选项更改为
!选项

<select 
  class="form-control"
  name="searchCode">
  <option value="0"> Nothing Selected</option>
  @foreach (string str in results) {
    string sel = "";
    if (str.Equals(searchCode)) {
      sel = "selected";
    }
    <!option 
      value="@str"
      @sel>
      @str - @sel
    </!option>
  }
</select>

未选择任何内容
@foreach(结果中的字符串str){
字符串sel=“”;
if(str.Equals(搜索码)){
sel=“已选定”;
}
@str-@sel
}
1)为搜索条件创建类

public class SearchCriteria
{
    public string searchCode { get; set; }
    public List<string> results { get; set; }
}
3) 获取结果后,通过传递对象(控制器)来绑定对象

ViewBag.criteria = searchCriteriaObject;
4) 查看页面:

<select asp-for="ViewBag.criteria.searchCode" 
         asp-items="ViewBag.criteria.results">
</select>


你为什么选择!@(sel==“”?“”:“selected”)上的选项最初类似于
sel=“selected=\”selected\”并以@(sel==”?:“selected=\“selected\”)的形式进行操作将很难理解,但由于出现了问题,我以精简版本结束,我也不知道我可以做像@(*)这样的操作,感谢tipI提出这个问题,因为“!option”是一个解决方案,但其他解决方案是标准代码。
ViewBag.criteria = searchCriteriaObject;
<select asp-for="ViewBag.criteria.searchCode" 
         asp-items="ViewBag.criteria.results">
</select>