MVC 6 - TagHelper选择 [英] MVC 6 - TagHelper Select

查看:386
本文介绍了MVC 6 - TagHelper选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我不知道这是为什么了选择TagHelper的默认行为。

This may be a stupid question but I do not know why this is the default behavior for the Select TagHelper.

这是我在我看来

<select asp-for="Estimators" asp-items="Model.Estimators" class="form-control"></select>

和这是在页面

<select class="form-control" id="Estimators" multiple="multiple" name="Estimators"><option value="2">Enio LastName</option>
<option value="6">Ianko Diaz</option>
<option value="7">Iordan Diaz</option>
<option value="8">Joan Alonso</option>
<option value="5">Lazaro Araya</option>
<option value="3">Leydis Martinez</option>
<option value="4">Ruben Cruz</option>
<option value="1">Shamir Ajate</option>
<option value="9">Yudiel Curbelo</option>
</select>

为什么选择标签渲染为多个=多​​个。

why is the select tag rendering as multiple="multiple".

推荐答案

您实际使用辅助错了。你应该有另外一个属性来存储所选择的项目。

You are actually using the helper wrong. You should have another property to store the selected item.

public class YourViewModel
{
   public int SelectedEstimator { set; get; }
   public List<SelectListItem> Estimators { set; get; }
}

和在你看来

@model YourViewModel
<select asp-for="SelectedEstimator" asp-items="@Model.Estimators">
    <option>Please select one</option>
</select>

这会使一个可选的SELECT元素。

This will render a single selectable SELECT element.

当您使用属性 ASP换的项目是数组类型,生成的选择元素将是多选。

When the property you use for asp-for items is of array type, the generated select element will be multi select.

public class YourViewModel
{
   public int[] SelectedEstimator { set; get; }
   public List<SelectListItem> Estimators { set; get; }
}

这篇关于MVC 6 - TagHelper选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆