ASP.NET MVC下拉列表没有选择渲染值?

时间:2017-05-11 12:33:24

标签: asp.net asp.net-mvc entity-framework asp.net-mvc-4

在模型=>

的帮助下从控制器发送选择列表以进行查看

喜欢以下=>

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

问题是视图中的选择列表没有显示我从控制器发送的选定值。 在视图中=> enter image description here 在上面我只输出检查供应商名称并显示=>看 它显示了VENDOR NAME ORACLE,但在下拉列表中,所选值是CISCO.why正在发生。

我的观看代码=>

@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>

和我的模特=&gt;

    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; }
}

我已经完成了我的全班模特,有很多不必要的工作人员。请忽略它。

请告诉我或建议我做错了什么?在哪里?

1 个答案:

答案 0 :(得分:0)

尝试定义下拉列表,如下面的代码

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