2010-04-23 65 views
0

爲什麼不能解決這個控制器方法網頁UI控件和ASP.NET MVC

public ActionResult Person() 
    { 
     testEntities6 testPersons = new testEntities6(); 
     IQueryable<person> persons; 

     DropDownLst.Items.Clear(); 
     DropDownLst.Items.Add("proba"); 

     persons = from i in testPersons.person 
        select i; 
     return View(persons); 
    } 

的查看頁面,包括命名空間:

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Mvc; 
    using System.Web.Mvc.Ajax; 
    using MvcKVteam.Models; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Web.UI.HtmlControls; 

鑑於網頁,我把這個代碼:

<asp:DropDownList ID="DropDownLst" runat="server"> 
    </asp:DropDownList> 

回答

1

我不認爲你可以在MVC中使用服務器控件。您應該使用HTML Helpers來代替。基於

在現有代碼:

<%= Html.DropDownList("DropDownLst", Model.AsEnumerable()) %> 

但我會建議使用視圖模型,而不是僅僅通過一個IQueryable的視圖。我也建議你拿起一本MVC書。專業的ASP.NET MVC 1.0非常好,但不包括2.0版本。

Rich

+0

我有錯誤;沒有類型爲'IEnumerable '的ViewData項具有鍵'DropDownLst'。 使用您的代碼後 – Ognjen 2010-04-23 08:18:27