0

I don't have clear why this is not working

public class Regiones
{
    [DisplayName("Select the region")]
    public int SelectedId { get; set; }
    public IEnumerable<SelectListItem> SelectedRegion { get; set; }
    public Regiones()
    {
        SelectedRegion = new[]{
            new SelectListItem { Value = "BR", Text = "Brasil" },
            new SelectListItem { Value = "EUNE", Text = "Europe East" },
            new SelectListItem { Value = "EUW", Text = "Europe West" },
            new SelectListItem { Value = "KR", Text = "Korea" },
            new SelectListItem { Value = "LAN", Text = "Latin America North" },
            new SelectListItem { Value = "LAS", Text = "Latin America South" },
            new SelectListItem { Value = "NA", Text = "Northamerica" },
            new SelectListItem { Value = "OCE", Text = "Oceania" },
            new SelectListItem { Value = "TR", Text = "Tr" },
            new SelectListItem { Value = "PBE", Text = "PBE" },
            new SelectListItem { Value = "Global", Text = "Global" }
        };
    }
}

Controller:

public class FeaturedGamesController : Controller   
{
    public ActionResult Index()
    {
        Regiones region = new Regiones();
        return View(region);
    }

    [HttpPost]
    public ActionResult Featured(Regiones region)
    {
        string selectedregion =             region.SelectedRegion.ElementAt(region.SelectedId).Text;
    }

View:

<div class="row">
<div class="col-md-12 text-center">
    @using (@Html.BeginForm("Featured", "FeaturedGames", FormMethod.Post, new { id = "form" }))
    {
        @Html.AntiForgeryToken();
        <div class="row">
            <div class="col-md-10 col-md-offset-2">
                <fieldset>
                    <div class="col-md-3">
                        @Html.LabelFor(x => x.SelectedId)
                    </div>

                    <div class="col-md-2">
                        @Html.DropDownListFor(x => x.SelectedId, Model.SelectedRegion, new { @id = "regiones" })
                    </div>

                    <div class="col-md-2">

                        <input type="submit" class="btn btn-default" value="Execute" />
                    </div>
                </fieldset>
            </div>
        </div>

    }
</div>

I have tried this: How to go to particular Item in IEnumerable and this: Pass SelectedValue of DropDownList in Html.BeginForm() in ASP.NEt MVC 3

When I click the sent the result is always 0 in the SelectedId...

What can I be missing? and thank you in advance

Community
  • 1
  • 1
Jose Ortega
  • 1,002
  • 13
  • 23
  • Did you try something like this `region.SelectedRegion.ToList().Where(x=>x.Value == region.SelectedId).Text` – Sateesh Pagolu Sep 14 '15 at 05:42
  • The point is that when is submiting the form. it looks like empty then "SelectedId is always = 0" and it doesn't get the coorect value in that. The model "region in the controller" (postback) it's always empty... – Jose Ortega Sep 14 '15 at 06:19

1 Answers1

2

The problem is your Value is string type in SelectListItem but your SelectedId is int type.

Value in below line is of type string

new SelectListItem { Value = "BR", Text = "Brasil" },

selectedid in below line of code is int.

[DisplayName("Select the region")]
    public int SelectedId { get; set; }

So, the value is lost in conversion. Change SelectedId's datatype to string to resolve the issue.

Fiddle here : https://dotnetfiddle.net/SDZZks

Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48