ASP.NET MVC如何显示DropDownList
ASP.NET MVC how to show DropDownList
我想在没有数据库的情况下将项目添加到 DropDownList 中。
但输出只显示 "Testing..."
我的模型class.
public class DropDownListData
{
public DropDownListData()
{
City = new List<SelectListItem> { };
}
public List<SelectListItem> City;
}
控制器
public ActionResult Travel()
{
DropDownListData ddld = new DropDownListData();
ddld.City = new List<SelectListItem>
{
new SelectListItem {Value = "Paris" ,Text = "Paris"},
new SelectListItem {Value = "Moscow",Text = "Moscow"},
new SelectListItem {Value = "Yerevan" ,Text = "Yerevan" }
};
return View(ddld);
}
查看
@model ASP.NET.Test.Models.DropDownListData
@{
Layout = null;
}
Testing...
@{
Html.DropDownList("series", Model.City, "Choose City");
}
为什么我没有看到 DropDownList。我做错了什么?
你必须使用:
@Html.DropDownList("series", Model.City, "Choose City")
而不是:
@{
Html.DropDownList("series", Model.City, "Choose City");
}
您所做的只是调用了 DropDownList 函数,没有对 MvcHtmlString
returned 执行任何操作。 ReSharper 实际上警告我未使用 return 值。
使用下面的代码直接按预期输出您的 DropDownList:
@Html.DropDownList("series", Model.City, "Choose City");
您当前的用法通常会作为 "code block" 发挥作用,如下例所示:
@{
// This code will be executed and can be used to set variables, etc.
var answer = 42;
}
<!-- This is an example of outputting your value -->
<p>
The answer to the question is <b>@answer</b>.
</p>
因此您当前的代码只会调用 Html.DropDownList()
方法,但绝不会实际使用或输出它。但是,在它前面加上 @
字符会将其视为表达式并相应地输出它。
我想在没有数据库的情况下将项目添加到 DropDownList 中。 但输出只显示 "Testing..."
我的模型class.
public class DropDownListData
{
public DropDownListData()
{
City = new List<SelectListItem> { };
}
public List<SelectListItem> City;
}
控制器
public ActionResult Travel()
{
DropDownListData ddld = new DropDownListData();
ddld.City = new List<SelectListItem>
{
new SelectListItem {Value = "Paris" ,Text = "Paris"},
new SelectListItem {Value = "Moscow",Text = "Moscow"},
new SelectListItem {Value = "Yerevan" ,Text = "Yerevan" }
};
return View(ddld);
}
查看
@model ASP.NET.Test.Models.DropDownListData
@{
Layout = null;
}
Testing...
@{
Html.DropDownList("series", Model.City, "Choose City");
}
为什么我没有看到 DropDownList。我做错了什么?
你必须使用:
@Html.DropDownList("series", Model.City, "Choose City")
而不是:
@{
Html.DropDownList("series", Model.City, "Choose City");
}
您所做的只是调用了 DropDownList 函数,没有对 MvcHtmlString
returned 执行任何操作。 ReSharper 实际上警告我未使用 return 值。
使用下面的代码直接按预期输出您的 DropDownList:
@Html.DropDownList("series", Model.City, "Choose City");
您当前的用法通常会作为 "code block" 发挥作用,如下例所示:
@{
// This code will be executed and can be used to set variables, etc.
var answer = 42;
}
<!-- This is an example of outputting your value -->
<p>
The answer to the question is <b>@answer</b>.
</p>
因此您当前的代码只会调用 Html.DropDownList()
方法,但绝不会实际使用或输出它。但是,在它前面加上 @
字符会将其视为表达式并相应地输出它。