-1

I'm using mvc4. How can I Bind CuntryName and its values in DropdownList Country?

public class Country
{
    public int Cnt_Id { get; set; }
    public string Cnt_Name { get; set; }
}

This is my private class

public class HybridEmployee
{
    public IEnumerable<Country> GetCount { get; set; }
}

Controller

public ActionResult GetCountry()
{
    var x = ObjRepo.GetCountry();
    hybrid.GetCount = x;
    return View(hybrid);
}

Index.cshtml

@model  Mvc_Application.Models.HybridEmployee
@using Mvc_Application.Models
@using (Html.BeginForm("SaveEmp", "Home", FormMethod.Post))
{
    @Html.DropDownListFor(x=>x.GetCount.FirstOrDefault().Cnt_Id),new SelectList(Model.GetCount,"","");
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • `new SelectList(Model.GetCount,"Cnt_Id","Cnt_Name")` but you also have your parenthesis wrong, and you cannot use `x=>x.GetCount.FirstOrDefault().Cnt_Id` - not only does it not make sense, it wont bind to anything anyway. You need a property to bind to, say `public int SelectedCountry { get; set; }` –  Oct 02 '17 at 07:46
  • @stephen could u plz reffer any demo –  Oct 02 '17 at 07:48
  • Suggest you refer the code in [this question/answer](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) to understand how to create and bind to a dropdownlist –  Oct 02 '17 at 07:48
  • Also see https://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor to find out how `DropDownListFor` model binding works - the first argument must not use LINQ with lambda expression, but a property from viewmodel that holding selected value. – Tetsuya Yamamoto Oct 02 '17 at 07:52

2 Answers2

1

We can have two approaches as shown below:

  1. Using a ViewBag containing the data for dropdown list.

Model file:

public class State
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    public int CountryID { get; set; }
}

In .cs file:

ViewBag.Countries = countryService.All().Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList();

In .cshtml file:

@Html.DropDownListFor(x => x.CountryID, ViewBag.Countries as IEnumerable<SelectListItem>, "Select Country", new { @class = "form-control" })
  1. Using a Model's property containing the data for dropdown list.

Model file:

public class State
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    public int CountryID { get; set; }

    public List<SelectListItem> Countries { get; set; }
}

In .cs file:

model.Countries = countryService.All().Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList();

In .cshtml file:

@Html.DropDownListFor(x => x.CountryID, Model.Countries, "Select Country", new { @class = "form-control" })
Reyan Chougle
  • 4,917
  • 2
  • 30
  • 57
  • Thank u soo much @Reyan –  Oct 02 '17 at 07:58
  • Recommending using `ViewBag` rather than a view model which OP already has is dreadful advice –  Oct 02 '17 at 08:02
  • Except that `int CountryID` and `IEnumerable Countries` should be in OP's `HybridEmployee` class (not sure what your `State` class is - it has nothing to do with OP's question) –  Oct 02 '17 at 08:43
  • @StephenMuecke State class is an example. I thought you would be able to understand it. – Reyan Chougle Oct 02 '17 at 08:45
0

This expression totally doesn't makes sense:

@Html.DropDownListFor(x=>x.GetCount.FirstOrDefault().Cnt_Id),new SelectList(Model.GetCount,"","");

The first argument of DropDownListFor helper (Expression<Func<TModel, TProperty>> expression) doesn't use LINQ expression, it is model binding expression - you must use a property to hold selected value instead. The drop down list binding should be used like this:

Model

public class Country
{
    public int Cnt_Id { get; set; }
    public string Cnt_Name { get; set; }
    public int SelectedCountry { get; set; } // additional property to get selected value
}

View

@Html.DropDownListFor(x => x.SelectedCountry, new SelectList(Model.GetCount, "Cnt_Id", "Cnt_Value"), null)
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61