[Solved] ASP.Net MVC Error: There is no ViewData item of type IEnumerable that has the key

yogeshc
 
on Sep 12, 2021 11:09 PM
407 Views

sir 

i am getting the error on post form 

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'MyCategory.id'.

@Html.DropDownListFor(model => model.MyCategory.id, ViewBag.catListLanguage as SelectList,"Select Category",   
public class MultiModelCate_pro
{
    public Category MyCategory { get; set; }
    public Product MyProduct { get; set; }
}

public class Product
{
    [Key]
    //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }
    [Required(ErrorMessage = " Please Select Language !!")]
    public string Language { get; set; }
    [DisplayName("Category Name")]
    [Required(ErrorMessage = " Category Name is Required")]
    public string C_name { get; set; }
    [DisplayName("Product Name")]
    [Required(ErrorMessage = " Product Name is Required")]
    public string Product_name { get; set; }
    [DisplayName("Description")]
    [Required(ErrorMessage = " Description is Required")]
    [AllowHtml]
    public string Description { get; set; }
    [DisplayName("Product Image")]
    public string Product_Image { get; set; }
    [NotMapped]
    public HttpPostedFileBase ImgFile { get; set; }
 }

public class Category
{
    [Key]
    // [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }
    [Required(ErrorMessage = " Please Select Language !!")]
    public string Language { get; set; }
 
    [DisplayName("Category Name")]
    [Required(ErrorMessage = " Category Name is Required")]
    public string C_name { get; set; }

    [DisplayName("Description ")]
    [Required(ErrorMessage = " Description is Required")]
    [AllowHtml]
    public string Description { get; set; }
    [DisplayName("Category Image ")]
    public string Category_Image { get; set; }
    [NotMapped]
    public HttpPostedFileBase ImgFile { get; set; }

    [NotMapped]
    public SelectList CategoryList { get; set; }
}

these are my model classes

@model Abhaimpex.Models.MultiModelCate_pro

@using (Html.BeginForm("Create", "Admin_Product", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    @Html.ValidationSummary(true, "", new { @class = "validator" })

    <section class="content">
        <div class="row">
            <!-- left column -->
            <div class="col-md-12">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Add Product </h3>
                        <a href="@Url.Action("Index", "Admin_Product")" class="btn btn-sm btn-primary" style="float:right">
                            <i class="fa fa-angle-left"></i>&nbsp; Back to Products List
                        </a>
                    </div><!-- /.box-header -->
                    <!-- form start -->
                    <form role="form">
                        <div class="box-body">
                            <div class="form-group">
                                <div class="col-md-2">
                                    @Html.DisplayNameFor(model => model.MyCategory.Language)
                                </div>
                                <div class="col-md-4">
                                    @Html.DropDownListFor(Model => Model.MyCategory.Language, new SelectList
                                   (new[] { "English", "Spanish", "portuguese" }),"Select Language", new { @class = "form-control" ,
                                       onchange = "redirect(this.value)" })
                                    @Html.ValidationMessageFor(model => model.MyCategory.Language, "", new { @class = "validator" })
                                </div>
                            </div><br /><br />
                            <div class="form-group">
                                <div class="col-md-2">
                                    @Html.DisplayNameFor(model => model.MyProduct.C_name)
                                </div>
                                <div class="col-md-4">
                                    @Html.DropDownListFor(model => model.MyCategory.id, ViewBag.catListLanguage as SelectList,"Select Category",                                 
                                    new { @class= "form-control", id="ddlcategory" })
                                    
                                    @Html.ValidationMessageFor(model => model.MyProduct.C_name, "", new { @class = "validator" })
                                </div>
                            </div><br /><br />
                            <div class="form-group">
                                <div class="col-md-2">
                                    @Html.DisplayNameFor(model => model.MyProduct.Product_name)
                                </div>
                                <div class="col-md-4">
                                    @Html.TextBoxFor(model => model.MyProduct.Product_name, new { @class = "form-control" })
                                    @Html.ValidationMessageFor(model => model.MyProduct.Product_name, "", new { @class = "validator" })
                                </div>
                            </div><br /><br />
                            <div class="form-group">
                                <div class="col-md-2">
                                    @Html.DisplayNameFor(model => model.MyProduct.Description)
                                </div>
                                <div class="col-md-10">
                                    @*@Html.TextBoxFor(model => model.Description, new { @class = "form-control" })*@
                                    @Html.TextAreaFor(model => model.MyProduct.Description, new { @class = "ckeditor" })
                                    @Html.ValidationMessageFor(model => model.MyProduct.Description, "", new { @class = "validator" })
                                </div>
                                <script>CKEDITOR.replace("Description");  </script>
                            </div><br /><br />
                            <div class="form-group">
                                <div class="col-md-2">
                                    @Html.DisplayNameFor(model => model.MyProduct.Product_Image)
                                </div>
                                <div class="col-md-4">
                                    <input type="file" id="img" name="ImgFile" accept="image/*" required>
                                </div>
                            </div><br /><br />
                           
                        </div><!-- /.box-body -->

                        <div class="box-footer">
                            <input type="submit" class="btn btn-primary" value="Submit" />
                           
                        </div>
                    </form>
                </div><!-- /.box -->
            </div>
        </div>
    </section><!-- /.content --> 

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 13, 2021 05:02 AM

Hi yogeshc,

You need to set the ViewBag.catListLanguag in the Index action to bind the DropDownList.

Refer below article for more details.

Populate (Bind) DropDownList from ViewBag in ASP.Net MVC