2013-05-17 82 views
-5

的形式進入到一個列表或集合如何記錄值我正在開發使用C#和SQL Server 2005在ASP MVC

我用的也是實體框架和準則第一種方法的ASP淨MVC 3應用程序。

我有這樣的形式: enter image description here

我想保存在列表中(或集合)entred值當我在按鈕「Enregistrer」點擊。

這是查看的代碼:

<fieldset class="parametrage"> 
     <legend>Gestion de Gamme</legend> 

     <div><%:Html.Label("Poste :")%><%: Html.DropDownList("SelectedPoste", Model.PostesItems)%><input type="checkbox" name="option1" value="Poste Initial" id= "chkMain" onclick="test();"/>Poste Initial<input type="checkbox" name="option2" value="Poste Final" id= "chkFirst" onclick="test2();"/>Poste Final</div> 


     <div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(x=>x.YourGammeModel.Nbr_Passage)%></div> 
     <div><%:Html.Label("Position :")%><%: Html.EditorFor(x=>x.YourGammeModel.Position)%></div> 
     <div><%:Html.Label("Poste Précédent :")%><%: Html.DropDownList("PostePrecedentSelected", Model.PostesItems)%></div> 
     <div><%:Html.Label("Poste Suivant :")%><%: Html.DropDownList("PosteSuivantSelected", Model.PostesItems)%></div> 


     <div><input type="submit" value="Enregistrer" id="btnSave" /></div> 

     </fieldset> 

視圖模型:

private static Dictionary<string, Gamme> userGammes; 

public static Dictionary<string, Gamme> UserGammes 
{ 
    get 
    { 
     if (userGammes == null) 
     { 
      userGammes = new Dictionary<string, Gamme>(); 
     } 
     return userGammes; 
    } 
} 

和控制器:

public ActionResult Save(Gamme gamme) 
    { 
     UserGammes.Add("currentUserID", gamme); 
    } 
+0

好吧。你有什麼嘗試?你卡在哪裏? –

+0

@OndrejTucny是的,我試過,,,請檢查我的更新,但在控制器中的聲明不接受VS – anouar

+0

我會建議在進入您自己的項目之前完成教程。 http://www.asp.net/mvc –

回答

1

一般來說,當你有你可以使用一個集合可以選擇的類似事物的列表(即,多選列表框)。 M in MVC是數據模型。沒有這個,它並不是真的有效。

您應該創建一個包含所需的字段,然後傳遞類到您的視圖類,如:

public class UserGammeModel { 
    public string PosteItems 
    public string NobreDePassage { get; set; } 
    public string Position { get; set; } 
    public Gamme PostePrecedent { get; set; } 
    public Gamme PosteSuivant { get; set; } 
} 

使用任何對象類型有意義的模型類的屬性 - 越strongly-鍵入,越好!

然後你的模型傳遞到控制器中的GET操作方法的觀點:

public ActionResult Save() { 
    return View(new UserGammeModel()); 
} 

最後,手柄控制器POST操作方法公佈值:

[HttpPost] 
public ActionResult Save(UserGammeModel model) { 
    // Do stuff with posted model values here 
} 
+0

thx彼得,但我會在哪裏將我的模型傳遞給視圖? – anouar

+1

在控制器的GET操作方法,如上所述...... –

+0

@Evanlewis問題,我已經有一個模型'Gamme'!所以我不會像Peter說的那樣創建一個新類,但是我必須將這些方法放在名爲AnouarController的Controller中,因爲它是填充表單視圖的方法 – anouar