2009-06-26 15 views
0

最近,我發佈了一個關於html幫手下拉列表的問題並使其工作(here)。但是現在我已經決定切換到ModelView模式是非常聰明的,所以我可以在我的視圖中訪問強類型的方法等。我所做的是我通過以下方式對我的其他主題中的代碼進行了一些調整:ASP MVC + Html.DropDownList()使用ModelView模式的問題

VacatureFormViewModel:

public class VacaturesFormViewModel 
{ 
    public Vacatures Vacature { get; private set; } 
    public SelectList EducationLevels { get; private set; } 
    public SelectList Branches { get; private set; } 
    public SelectList CareerLevels { get; private set; } 

    Repository repository; 

    // Constructor 
    public VacaturesFormViewModel(Vacatures vacature) 
    { 
     this.Vacature = vacature; 
     this.repository = new Repository(); 
     this.EducationLevels = new SelectList(repository.GetAllEducationLevels(),"ID","Name",vacature.EducationLevels); 
     this.Branches = new SelectList(repository.GetAllBranches(),"ID","Name",vacature.Branches); 
     this.CareerLevels = new SelectList(repository.GetAllCareerLevels(), "ID", "Name", vacature.CareerLevels); 

    } 
} 

BanenController:

// 
    // GET: /Banen/Create 

    public ActionResult Create() 
    { 
     Vacatures vacature = new Vacatures(); 
     return View(new VacaturesFormViewModel(vacature)); 
    } 

    // 
    // POST: /Banen/Create 

    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Create(Vacatures vacatureToAdd) 
    { 
     if (ModelState.IsValid) 
     { 
      try 
      { 
       // TODO: Add insert logic here 
       repository.AddToVacatures(vacatureToAdd); 
       repository.SaveChanges(); 

       // Return to listing page if succesful 
       return RedirectToAction("Index"); 
      } 
      catch (Exception e) 
      { 
       return View(); 
      } 
     } 
    } 

而且我Create.aspx視圖(一部分):

<% using (Html.BeginForm()) {%> 

    <fieldset> 
     <legend>Fields</legend> 
     <p> 
      <label for="Title">Title:</label> 
      <%= Html.TextBox("Title", Model.Vacature.Title) %> 
      <%= Html.ValidationMessage("Title", "*") %> 
     </p> 
     <p> 
      <label for="Content">Content:</label> 
      <%= Html.TextArea("Content", Model.Vacature.Content) %> 
      <%= Html.ValidationMessage("Content", "*") %> 
     </p> 
     <p> 
      <label for="EducationLevels">EducationLevels:</label> 
      <%= Html.DropDownList("EducationLevels", Model.EducationLevels)%> 
      <%= Html.ValidationMessage("EducationLevels", "*") %> 
     </p> 
     <p> 
      <label for="CareerLevels">CareerLevels:</label> 
      <%= Html.DropDownList("CareerLevels", Model.CareerLevels)%> 
      <%= Html.ValidationMessage("CareerLevels", "*")%> 
     </p> 
     <p> 
      <label for="Branches">Branches:</label> 
      <%= Html.DropDownList("Branches", Model.Branches)%> 
      <%= Html.ValidationMessage("Branches", "*")%> 
     </p> 
     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 
<% } %> 

爲了指導,我使用了ScottGu的NerdDinner教程,我已經閱讀了各種主題。

我的問題是,如果有可能讓MVC ASP自動設置我的職業等級,educationlevel和分支(dropdownlists),因爲它目前正在返回一個不是我想要的ID字符串。當我改變了創作的SelectList的:

this.CareerLevels = new SelectList(repository.GetAllCareerLevels(), vacature.CareerLevels); 

所以沒有「ID」和「名稱」不救不(我猜它仍然返回爲POST方法的字符串,而不是對象本身),並在此旁邊,它在視圖中列出爲:vacature.EducationLevels等。所以不是名稱,但對象本身列出。

最後一個問題 因此,簡而言之,我的問題是,如果有可能用這種方式來設置我的領域,educationallevel和careerlevel。那麼不是自動的?

在這種情況下,我還是要用的東西,如:

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Create(FormCollection form) 
    { 
     Vacatures vacatureToAdd = new Vacatures(); 

     // Retrieve the education level by its ID 
     if (!form["EducationLevels"].Equals("")) 
     { 
      Guid educationID = new Guid(form["EducationLevels"]); 
      vacatureToAdd.EducationLevels = repository.GetEducationLevelByID(educationID); 
     } 

在我的控制?或者還有其他更流暢的選擇。

回答

2

編輯使用GUID:

隨着dropdownlists我用稍微不同的方法。有可能讓您的視圖模型的工作,但對我來說更容易這樣:

public class VacaturesFormViewModel 
{ 

    public IEnumerable<SelectListItem>EducationLevels{ get; set; } 
    public Guid EducationLevelID{ get; set; } 

} 

的EducationLevelID將有你的下拉的選擇的ID。 這是視圖:

<%= Html.DropDownList("EducationLevelID", Model.EducationLevels)%> 

控制器

IEnumerable<SelectListItem> educationLevelList = 
          from level in GetLevelList() 
          select new SelectListItem 
          { 
           Text = level .Name, 
           Value = level.Uid.ToString() 
          }; 
    model.EducationLevels = educationLevelList ; 
1

我不確定,但我認爲你應該創建模型粘合劑。 (David Hayden寫了一個簡單的模型粘合劑)

+0

這也是基於字符串僅信息的例子。當他使用: HttpContext.Request。表格[ 「名稱」]; 在我的情況下,究竟是哪裏出了問題,這不會給我想象的對象,而是一個字符串。但不知道。 – bastijn 2009-06-26 08:48:09

+0

另一方面,這可能實際上的工作方式如下: > Blockquote「您可以爲客戶創建自己的自定義模型綁定器,以實例化新的客戶實例,並將相應的表單值添加到各種客戶屬性中。 – bastijn 2009-06-26 08:51:25

1

你可以結合自動educationID參數:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Create(Guid? educationID, FormCollection form) 
{ 
    Vacatures vacatureToAdd = new Vacatures(); 

    if (educationID != null) 
    { 
     vacatureToAdd.EducationLevels = 
      repository.GetEducationLevelByID(educationID.Value); 
    }