Home > Backend Development > C++ > How to Set the Selected Item in an MVC5 Razor @Html.DropDownListFor when the Value is in an Array?

How to Set the Selected Item in an MVC5 Razor @Html.DropDownListFor when the Value is in an Array?

Mary-Kate Olsen
Release: 2025-01-30 00:26:09
Original
503 people have browsed it

How to Set the Selected Item in an MVC5 Razor @Html.DropDownListFor when the Value is in an Array?

In MVC5 Razor, when the value of@html.dropdownListfor is an array, how to set the selection item?

ASP.NET MVC Auxiliary method is used to generate a drop -down list for the attributes in the model. When the model attribute is an array or collection, the drop -down list will be presented to each item in the array or collection.

However, when the value is in the array or set, the selection value of the drop -down list may be tricky. By default, the selected value will be based on the first settings in the array or collection. @Html.DropDownListFor

To set the selection value of the value in the array or collection, you can use the following two methods:

Method 1: Use the editor template

Create a custom editor template for the type in the collection. Create a part of the view in , including the following code:

Then in the main view, send Selectlist as an additional ViewData to the editor template:

Views/Shared/EditorTemplates/AggregationLevelConfiguration.cshtml

Method 2: Generate a new Selectlist in each iteration and set the selection value
<code class="language-csharp">@model yourAssembly.AggregationLevelConfiguration
@Html.DropDownListFor(m => m.HelperCodeType, (SelectList)ViewData["CodeTypeItems"])
.... // AggregationLevelConfiguration的其他属性</code>
Copy after login

In this method, set the
<code class="language-csharp">@using (Html.BeginForm())
{
  ...
  @Html.EditorFor(m => m.Configurations , new { CodeTypeItems = Model.CodeTypeItems })
  ...
}</code>
Copy after login
attribute to

rather than . Then in the main view, generate a new :

CodeTypeItems IEnumerable<genericidnametype> Please note that this method is only applicable to the first element of the SelectList array. For other elements in the array, cycle treatment is required and Configurations is generated for each element. This may need to use the circular structure (such as SelectList cycle or cycle) to iterate the array, and generate a new

in each iteration. This will ensure that each drop -down list is set correctly.
<code class="language-csharp">@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, new SelectList(Model.CodeTypeItems, "Id", "Name", Model.Configurations[0].HelperCodeType))</code>
Copy after login

The above is the detailed content of How to Set the Selected Item in an MVC5 Razor @Html.DropDownListFor when the Value is in an Array?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template