HTML.DropDownListFor 下拉列表
HTML.DropDownListFor DropDownList
我正在尝试将数据从我的数据库检索到 HTML,而不是检索到 Html.DropDownListFor,但我无法检索到标记。
NewCustomerViewModel
public class NewCustomerViewModel
{
public int CustId { get; set; }
[Required]
public string CustFirstName { get; set; }
[Required]
public string CustLastName { get; set; }
[Required]
public int StId { get; set; }
public IEnumerable<State> States { get; set; }
}
客户控制器
public class CustomerController : Controller
{
private CustomerDbContext _context;
public CustomerController(CustomerDbContext context)
{
_context = context;
}
// GET: /<controller>/
public IActionResult Index()
{
return View(_context.Customers.ToList());
}
public IActionResult Create()
{
var stateNames = _context.States.ToList();
var viewModel = new NewCustomerViewModel
{
States = stateNames
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Customer customer)
{
if (ModelState.IsValid)
{
_context.Customers.Add(customer);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(customer);
}
}
创建视图
下面的 HTML DropDownListFor 工作正常:
@Html.DropDownListFor(m => m.StId, new SelectList(Model.States, "StId", "StName"))
我无法让 select 标签工作。
<select asp-for="StId" asp-items="@Model.States" class="form-control">
<option>Select State</option>
</select>
我的所有 HTML 在我的创建视图中使用而不是 HTML 助手,这是我试图避免的。我只是希望能够将数据检索到标签。
对于 select 标签助手,asp-items
期望 SelectListItem
collection\SelectList,其中的每个项目都有一个 Value
和 Text
属性。 Value
属性值将用于选项的值,Text
属性值将用于 UI.
中选项的显示文本
您 States
collection 中的项目没有值和文本 属性,但有 StId
和 StName
属性。所以我们需要把这个类型转换成SelectListItem类型。
所以你的代码应该是
<select asp-for="StId" asp-items="@(new SelectList(Model.States,"StId","StName"))">
<option>Please select one</option>
</select>
附加参考
我正在尝试将数据从我的数据库检索到 HTML,而不是检索到 Html.DropDownListFor,但我无法检索到标记。
NewCustomerViewModel
public class NewCustomerViewModel
{
public int CustId { get; set; }
[Required]
public string CustFirstName { get; set; }
[Required]
public string CustLastName { get; set; }
[Required]
public int StId { get; set; }
public IEnumerable<State> States { get; set; }
}
客户控制器
public class CustomerController : Controller
{
private CustomerDbContext _context;
public CustomerController(CustomerDbContext context)
{
_context = context;
}
// GET: /<controller>/
public IActionResult Index()
{
return View(_context.Customers.ToList());
}
public IActionResult Create()
{
var stateNames = _context.States.ToList();
var viewModel = new NewCustomerViewModel
{
States = stateNames
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Customer customer)
{
if (ModelState.IsValid)
{
_context.Customers.Add(customer);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(customer);
}
}
创建视图
下面的 HTML DropDownListFor 工作正常:
@Html.DropDownListFor(m => m.StId, new SelectList(Model.States, "StId", "StName"))
我无法让 select 标签工作。
<select asp-for="StId" asp-items="@Model.States" class="form-control">
<option>Select State</option>
</select>
我的所有 HTML 在我的创建视图中使用而不是 HTML 助手,这是我试图避免的。我只是希望能够将数据检索到标签。
对于 select 标签助手,asp-items
期望 SelectListItem
collection\SelectList,其中的每个项目都有一个 Value
和 Text
属性。 Value
属性值将用于选项的值,Text
属性值将用于 UI.
您 States
collection 中的项目没有值和文本 属性,但有 StId
和 StName
属性。所以我们需要把这个类型转换成SelectListItem类型。
所以你的代码应该是
<select asp-for="StId" asp-items="@(new SelectList(Model.States,"StId","StName"))">
<option>Please select one</option>
</select>
附加参考