显示可编辑下拉菜单中的默认选择值。

huangapple go评论91阅读模式
英文:

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 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.

英文:

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>
英文:
&lt;input id=&quot;ProductType&quot; list=&quot;ProductTypeList&quot; name=&quot;ProductType&quot; placeholder=&quot;@Model.ProductType&quot;&gt;

&lt;datalist id=&quot;ProductTypeList&quot;&gt;
    @foreach (var item in Model.ProductTypeList)
    {
        @if (item == Model.ProductType)
        {
            &lt;option value=&quot;@item&quot; selected&gt;@item&lt;/option&gt;
           
        }
        else
        {
            &lt;option value=&quot;@item&quot; &gt;@item&lt;/option&gt;
        }

       
    }
&lt;/datalist&gt;

huangapple
  • 本文由 发表于 2023年6月13日 03:27:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定