发布下拉列表时出错

时间:2017-05-15 05:58:47

标签: c# asp.net-mvc razor model-view-controller html.dropdownlistfor

我收到以下错误:

  

没有类型为'IEnumerable'的ViewData项具有键'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 个答案:

答案 0 :(得分:0)

在您的控制器中将<iframe height="100%" width="100%" name="iframe_a"> <asp:ContentPlaceHolder ID="ContentPlaceHolder" runat="server"> </asp:ContentPlaceHolder> </iframe> 更改为ViewBag.DropdownValues并在视图中

控制器

ViewBag.Category

查看

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