0

sending a selectlist from controller to view with the help of a model=>

like below=>

ViewBag.vendor_heading = new SelectList(db.Vendors , "heading", "heading", course.vendor_heading);
return View(course);

the problem is in view the selectlist is not showing the selected value which i am sending from controller. in view => enter image description here in above i just output to check that vendor name and display for =>look it showing VENDOR NAME ORACLE but in the dropdown list the selected value is CISCO.why that is happening .

my view code=>

@model Course

<div>
    @Html.LabelFor(model => model.vendor_heading, "vendor Name", new { @class = "control-label" })
    @Html.DisplayFor(model => model.vendor_heading)
</div>
<div>
     @Html.LabelFor(model => model.vendor_heading, "vendor Name", new { @class = "control-label" })
     @Html.DropDownList("vendor_heading", (SelectList)ViewBag.vendor_heading, new { @class = "form-control input-lg", @style = "min-height:45px;" })
     @Html.ValidationMessageFor(model => model.vendor_heading, null, new { @class = "text-danger" })
 </div>

and my model=>

    public class Course
{
    [MaxLength(700, ErrorMessage = "The Max Length For Heading Is 700 Character!")]
    public string vendor_heading { get; set; }
    /// <summary>
    /// //////////
    /// </summary>
    [Key]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Course Name Is Required!")]
    [MaxLength(700, ErrorMessage = "The Max Length For Course Name Is 700 Character!")]
    [Display(Name = "Course Name")]
    public string name { get; set; }
    /// <summary>
    /// //////////
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessage = "Course Code Is Required!")]
    [MaxLength(200, ErrorMessage = "The Max Length For Course Code Is 200 Character!")]
    [Display(Name = "Course Code")]
    public string code { get; set; }
    /// <summary>
    /// //////////
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessage = "Picture Is Required!")]
    [MaxLength(1000, ErrorMessage = "The Max Length For Pic Path Is 1000 Character!")]
    [Display(Name = "Pic")]
    public string pic_path { get; set; }
    /// <summary>
    /// /////////
    /// </summary>
    [Required(ErrorMessage = "Adding date Is Required!")]
    [DataType(DataType.Date, ErrorMessage = "Invalid Date!")]
    [Display(Name = "Adding Date")]
    public DateTime adding_date { get; set; }
    /// <summary>
    /// /////////
    /// </summary>
    [MaxLength(5000, ErrorMessage = "The Max Length For Course Details Is 5000 Character!")]
    [Display(Name = "Course Details")]
    public string details { get; set; }

    //relationship With  other tables-------
    [ForeignKey("vendor_heading")]
    public Vendor Vendor { get; set; }
    public List<Batche> Batches { get; set; }
}

i have give my full model class, there are a lots of unnecessary staff. please ignore that.

please tell me or suggest me what i have done wrong and where?

nayan chowdhury
  • 283
  • 1
  • 9
  • 28

1 Answers1

0

Try to define dropdownlist like below code

 @Html.DropDownList("vendor_heading", (SelectList)ViewBag.vendor_heading,htmlAttributes: new { @class = "form-control input-lg", @style = "min-height:45px;" })
Biby Augustine
  • 425
  • 1
  • 3
  • 16