如何在mvc中的@html.Dropdown中预填充Db中的选定值

时间:2014-06-05 06:16:20

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

我想在mvc的Dropdown列表中预先填充存储在DB中的选定值。

  

控制器

public ActionResult POCallDown(int? skuid)
{
    var supplierlist = db.QuoteMasters.Include(e => e.CommunicationMode).Include(e => e.SKUMaster).Include(e => e.EmployeeMaster).Include(e => e.SupplierMaster).Include(e => e.CreditTerm);
    var SupplierforSKU = (from supplierdetails in supplierlist.ToList()
                                  where skuid.HasValue && skuid.Value == supplierdetails.SKU
                                  select supplierdetails).ToList();

    foreach(var cred in SupplierforSKU)
    { 
        ViewBag.CreditId = new SelectList(db.CreditTerms, "CreditId", "Description",cred.CreditTermId);
    }
    return View(SupplierforSKU);

}
  

查看

<table>
<th>Credit</th> 
<tr><td> @Html.DropDownList("CreditId")</td></tr>
</table>

我在db中获取视图中所有行的第一个值。请帮忙。我尝试使用这个Populate dropdown ,但它不能给我想要的结果。

1 个答案:

答案 0 :(得分:0)

试试这个:

    <table>
     <th>Credit</th> 
       <tr>
         <td> 
            @Html.DropDownList("CreditId", null, "--Select One--", new { @id = "CreditId", @name ="CreditId"})
         </td>
       </tr>
    </table>