Ошибка при публикации выпадающего

Я получаю следующую ошибку:

Нет элемента ViewData типа 'IEnumerable', который имеет ключ 'Category'.

Строка 34:div class="form-group">

Строка 35:

Строка 36:@Html.DropDownList("Категория", (IEnumerable)ViewBag.DropdownValues,"Выбрать категорию", новый { @class = "form-control" })

Строка 37:

Строка 38:

Модель:

 public class CateogryList
{
    public string categoryValues { get; set; }

    public IEnumerable<SelectListItem> categoryValuesList { get; set; }
}

контроллер:

 public ActionResult CreateService()
    {

        CateogryList cate = new CateogryList();
        ServiceProviderRepository values = new ServiceProviderRepository();
        List<string> details = new List<string>(values.displayCateogry());
        var types = new List<SelectListItem>();
        int counter = 0;
        foreach (string val in details)
        {
            types.Add(new SelectListItem() { Text = val, Value = Convert.ToString(counter) });
            counter += 1;
        }
        ViewBag.DropdownValues = new SelectList(details, cate.categoryValues, "");
        return View();
    }
    [HttpPost]
    public ActionResult CreateService(ServicesViewModel addService, FormCollection form)
    {

        ViewData["SelectedItem"] = form["Category"].ToString();

        // ViewData["SelectedItem"] = HttpContext.Request.QueryString["Category"];
        return View();
    }

Servicerepository:

public class ServiceProviderRepository
{
    private SqlConnection con;
    /*
     * To Handle Database connection related activities
     * @parameter: No parameter
     */
    private void connection()
    {
        string constr = ConfigurationManager.ConnectionStrings["Event_Database"].ToString();
        con = new SqlConnection(constr);
    }

    public List<string> displayCateogry()
    {

        List<string> list = new List<string>();
        connection();
        string SQLquery = "Select Category from category";
        SqlCommand com1 = new SqlCommand(SQLquery, con);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(com1);
        DataTable dt = new DataTable();
        da.Fill(dt);
        foreach(DataRow row in dt.Rows)
        {
            list.Add(row.Field<string>(0));
        }

        return list;
    }
}

Посмотреть:

@model Event_Planner_MVC_Application.Models.ServicesViewModel

@{
ViewBag.Title = "CreateService";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create Service</h2>

@using (Html.BeginForm("CreateService", "ServiceProvider", FormMethod.Post)) 

{
@Html.AntiForgeryToken()
<h3>You have Selected: @ViewData["SelectedItem"]</h3>
<div class="form-horizontal">
    <h4>ServicesViewModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { 
@class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes 
= new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new { 
@class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Cost, htmlAttributes: new { @class = 
"control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Cost, new { htmlAttributes = new 
{ @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Cost, "", new { @class 
= "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="form-group">

            @Html.DropDownList("Category", 
(IEnumerable<SelectListItem>)ViewBag.DropdownValues,"Select Category", new { 
@class = "form-control" })
        </div>

    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

}

1 ответ

В вашем контроллере смени ViewBag.DropdownValues в ViewBag.Category и вид

контроллер

ViewBag.Category= new SelectList(details, cate.categoryValues, "");

Посмотреть

@Html.DropDownList("Category", 
(IEnumerable<SelectListItem>)ViewBag.Category,"Select Category", new { 
@class = "form-control" })
Другие вопросы по тегам