英文:
Showing default selected value in editable dropdown
问题
I have an editable dropdown list in the cshtml page as follows.
I need to show "PT2" as the default selected value in the dropdown list instead of "Select."
@Html.DropDownListFor(
model => model.ProductType, new SelectList(model.ProductTypeList, "")
)
Model:
public class Product
{
public string ProductType {get; set;}
public List
}
Example values in ProductTypeList: "PT1", "PT2", "PT3"
Wondering if this is possible in the above code? Any thoughts or pointers please??
Thank you.
英文:
I've an editable dropdownlist in the cshtml page as follows.
I need to show "PT2" as the default selected value in the dropdownlist instead of "Select".
<input id="ProductType" list="ProductTypeList" name="product-type-list" placeholder="Select">
@Html.DropDownListFor(
model => model.ProductType, new SelectList(model.ProductTypeList,"")
)
Model:
public class Product
{
public string ProductType {get; set;}
public List<string> ProductTypeList {get; set;}
}
Example values in ProductTypeList : "PT1", "PT2", "PT3"
Wondering if this is possible in the above code? Any thoughts or pointers please??
Thank you.
答案1
得分: 0
<input id="ProductType" list="ProductTypeList" name="ProductType" placeholder="@Model.ProductType">
<datalist id="ProductTypeList">
@foreach (var item in Model.ProductTypeList)
{
@if (item == Model.ProductType)
{
<option value="@item" selected>@item</option>
}
else
{
<option value="@item">@item</option>
}
}
</datalist>
英文:
<input id="ProductType" list="ProductTypeList" name="ProductType" placeholder="@Model.ProductType">
<datalist id="ProductTypeList">
@foreach (var item in Model.ProductTypeList)
{
@if (item == Model.ProductType)
{
<option value="@item" selected>@item</option>
}
else
{
<option value="@item" >@item</option>
}
}
</datalist>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论