-1

I have this code in my Controller:

 [HttpGet]
    public ActionResult Create()
    {
        List<SelectListItem> objResult = new List<SelectListItem>();
        Models.Countries country = new Models.Countries();
        DataTable result = country.GetAllCountries();

        foreach(DataRow row in result.Rows)
        {
            objResult.Add(new SelectListItem
            {
                Text = row["country"].ToString(),
                Value = row["id"].ToString()
            });
        }

        ViewBag.country = objResult;
        return View();
    }

and then within my view I have:

@model Project.Models.CountryViewModel
@Html.DropDownList("country", (IEnumerable<SelectListItem>)ViewBag.country, "Select")

And then I render this partial view within another view:

@model Project.Models.Register
@using (Ajax.BeginForm("Register", "Register", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "complete"}))
{
  <div id="complete">
  @Html.Partial("~/Views/Shared/Countries.cshtml, new Project.Models.CountryViewModel()")
  </div>
}

Within CountryViewModel I have:

public int CountryID { set; get; }
public string country { set; get; }

However I get this error:

Additional information: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'country'.

Does anyone know what I am doing wrong?

KTOV
  • 559
  • 3
  • 14
  • 39
  • Just copied and pasted your code it worked fine. – Shyju Feb 03 '16 at 15:00
  • 1
    looks like your error from another part of View – teo van kot Feb 03 '16 at 15:01
  • @teovankot The DropDownList is within a Partial View, are you saying other View's are interferring? – KTOV Feb 03 '16 at 15:06
  • It depend on how you load your partial View. If you load it with ajax - not. But if you using something like `@RenderPartial` - could be – teo van kot Feb 03 '16 at 15:08
  • Is your partial view strongly typed to some class ? Are you passing something to the partial view ? Show us the relevant code please – Shyju Feb 03 '16 at 15:08
  • @Shyju I updated my question – KTOV Feb 03 '16 at 15:10
  • Your code still works for me ! Your problem is somewhere else ! – Shyju Feb 03 '16 at 15:17
  • @Shyju I don't understand where it is getting that error from, their is no where else – KTOV Feb 03 '16 at 15:25
  • The error means that `ViewBag.country` is `null`. Possibly because you submit the form and return the view but did not reassign the value of `ViewBag.country` property as you did in the GET method. –  Feb 04 '16 at 00:56
  • @KTOV, I have marked this as a duplicate because the answer explains how to solve the issue. You get a slightly different error message because you have multiple other errors in your code. you cannot name the property your binding to the same as the `ViewBag` property you assign the `SelectList` to (rename the `ViewBag` property to say `CountryList`) but you really should be using a strongly typed view model anyway i.e. you view model contains a property `IEnumerable CountryList`. –  Feb 05 '16 at 00:33
  • You next problem is you bind the selected value (which is the `id` property of `Country` which I assume is `int`) to the `string country` property of your model. I assume you want to bind it to the `CountryID` property, and your model should not contain `string country` –  Feb 05 '16 at 00:35

1 Answers1

1

The render the partial view as

@Html.Partial("_YourPartialViewName", Model)

not like

 @Html.Partial("~/Views/Shared/Countries.cshtml, new Project.Models.CountryViewModel()")

You have give pass the Model object as in above code, then only the dropdown renders correctly.
Then Change the code as below in the controller

ViewBag.country = new SelectList(objResult, "Value", "Text");

Then in the partial view page

@model IEnumerable<Project.Models.CountryViewModel>
@Html.DropDownList("country", ViewBag.country as SelectList, "Select")

Hope this helps

anand
  • 1,559
  • 5
  • 21
  • 45