Kevin Kevin - 1 month ago 8
ASP.NET (C#) Question

C# MVC Dropdownlist no ViewData item

I am having an issue with a dropdownlist I have in a

PartialView
.

Basically get the following error
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Status'.


Here is my
ActionResult
method inside the controller

[HttpGet]
public ActionResult Status()
{
List<SelectListItem> Status = new List<SelectListItem>();

Status.Add(new SelectListItem { Text = "Open", Value = "1" });
Status.Add(new SelectListItem { Text = "Closed", Value = "2" });
Status.Add(new SelectListItem { Text = "Delete", Value = "3" });

ViewData["Status"] = Status;

return View();
}


My
Status
Partial View where I call the select list

@Html.DropDownList("Status", ViewData["Status"] as SelectList)


And then I call the partial view from my main view as follows

@Html.Partial("Status")


I am just not sure why its giving the above error. I took at look at this asp.net mvc dropdownlist no ViewData item and still not able to rectify the issue?

Update

As per @MikeDebela solution my Status Action Item was never been called. So used the following syntax to call the Action directly

@{Html.RenderAction("Status");}

Answer

When you use Partial helper, the runtime looks for the view and renders a string (your action will never executed). RenderAction on the other hand, executes the action and displays the result. So use:

@{Html.RenderAction("Status");}
Comments