2
I am trying to fill in with the selected value one DropDownList
within a for
, but it’s not working properly.
What happens is that when the Action Index
returns the model to a View
, values are not coming selected, always getting the first item of DropDownList
selected.
I set a small example to facilitate.
I edited adding @Richarddias suggestions
To help, I committed to Github https://github.com/pablotdv/TesteDropDownEnum
Classes
public class Principal
{
public MeuEnum Item { get; set; }
public List<Teste> Teste { get; set; }
}
public class Teste
{
public MeuEnum item { get; set; }
}
public enum MeuEnum
{
Item0 = 0,
Item1 = 1,
Item2 = 2,
Item3 = 3,
Item4 = 4,
Item5 = 5,
Item6 = 6,
Item7 = 7,
Item8 = 8,
}
public static class Helpers
{
public static SelectList MeuEnumItens(this HtmlHelper html)
{
var item0 = new SelectListItem() { Value = "Item0", Text = "0" };
var item1 = new SelectListItem() { Value = "Item1", Text = "1" };
var item2 = new SelectListItem() { Value = "Item2", Text = "2" };
var item3 = new SelectListItem() { Value = "Item3", Text = "3" };
var item4 = new SelectListItem() { Value = "Item4", Text = "4" };
var item5 = new SelectListItem() { Value = "Item5", Text = "5" };
var item6 = new SelectListItem() { Value = "Item6", Text = "6" };
var item7 = new SelectListItem() { Value = "Item7", Text = "7" };
var item8 = new SelectListItem() { Value = "Item8", Text = "8" };
return new SelectList(new[] { item0, item1, item2, item3, item4, item5, item6, item7, item8 }, "Value", "Text");
}
}
Controller
public class HomeController : Controller
{
public ActionResult Index()
{
Principal p = new Principal()
{
Item = MeuEnum.Item6
};
p.Teste = new List<Teste>();
p.Teste.Add(new Teste() { item = MeuEnum.Item0 });
p.Teste.Add(new Teste() { item = MeuEnum.Item1 });
p.Teste.Add(new Teste() { item = MeuEnum.Item2 });
p.Teste.Add(new Teste() { item = MeuEnum.Item3 });
return View(p);
}
}
View
@model TesteDropDownEnum.Controllers.Principal
@using TesteDropDownEnum.Controllers
@{
ViewBag.Title = "Home Page";
}
<!--Assim funciona-->
@Html.DropDownListFor(a => a.Item, Html.MeuEnumItens(), "Selecione", htmlAttributes: new { @class = "form-control", @readonly = "readonly" })
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Teste.Count(); i++)
{
<div class="row form-group">
<div class="col-md-12">
<label>Origem da Mercadoria @Model.Teste[i].item</label>
<!--Assim não funciona-->
@Html.DropDownListFor(a => a.Teste[i].item, Html.MeuEnumItens(Model.Teste[i].item), "Selecione", htmlAttributes: new { @class = "form-control", @readonly = "readonly" })
</div>
</div>
}
}
In the edition you made, in the method
MeuEnumItens
you must check that the received item is equal to the Enum item.– Richard Dias
I modified the method
MeuEnumItens
in my reply to give an example.– Richard Dias
I had already made the change you made, but it didn’t solve either.
– Pablo Tondolo de Vargas
I modified the return type of the Meuenumitens3 method for a Ienumerable<Selectlistitem> and returned the list directly and the problem was solved.
– Richard Dias
The answer has been edited. Try to replicate the edit to the Meuenumitens3 method and you will see that it will work. Here it worked with your GIT code.
– Richard Dias
I created a Fork in Git, if you want to take a look there.
– Richard Dias
@Richarddias we will look yes.
– Pablo Tondolo de Vargas
I made a pull request with the @Richarddias + Begincollectionitem response. Only one merge.
– Leonel Sanches da Silva
@Is this modification so that the names of the camps are correct? Would have the real need to use the partialview or could do the
using(Html.BeginCollectionItem)
before we go and play Dropdown cool? I wonder why I never used this package and probably soon I will need a project, and I want to take advantage and do it this way if it will save work.– Richard Dias
@Richarddias You can do without the Partial, but it gets harder because of the fields with Expression. What you need is just call me on chat or call me on Facebook that I help you.
– Leonel Sanches da Silva