2013-05-19 34 views
0

我正在開發使用C#和SQL Server 2005如何繼承型動物模型在同一視圖中的ASP MVC

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

我有一個包含窗體(文本框和列表框)的部分視圖'Gestion.ascx'。

該視圖使用ViewModel'FlowViewModel'。

我想這個視圖使用另一個模型'Gamme',我已經有了。

我試着把它們都放在'繼承標記'中,但是一些語句在RED中被加下劃線。

逸岸,說明更多的問題:

我用FlowViewModel在這個局部視圖,以便在我的名單boxfrom型動物模型加載一些數據。

現在我想存儲選定和進入本地變量的值。

我不能從模型'Gamme'傳遞給控制器​​,因爲視圖'Gestion'沒有使用模型'Gamme'。

這是局部視圖 'GESTIÓN' 的代碼:

<%@ Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.Models.FlowViewModel>" %> 

<% using (Html.BeginForm("Save", "Anouar")) { %> 
    <%: Html.ValidationSummary(true) %> 
    <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> 
<% } %> 

,這是該模型的FlowViewModel'的代碼:

namespace MvcApplication2.Models 
{ 
    public class FlowViewModel 
    { 

     [Key] 
     public string IDv { get; set; } 
     [NotMapped] 
     public SelectList PostesItems { get; set; } 

     public List<Profile_Ga> Profile_GaItems { get; set; } 
     public List<Gamme> GaItems { get; set; } 

     public Gamme YourGammeModel { get; set; } 

     public int SelectedProfile_Ga { get; set; } 

     public int SelectedGamme{ get; set; } 

     public int SelectedPoste { get; set; } 

     public int PostePrecedentSelected { get; set; } 
     public int PosteSuivantSelected { get; set; } 
} 
} 

,這是模式「的Gamme ':

public class Gamme 
    { 
     [Key] 
     [Column(Order = 0)] 
     [ForeignKey("Profile_Ga")] 
     public string ID_Gamme { get; set; } 
     [Key] 
     [Column(Order = 1)] 
     [ForeignKey("Poste")] 
     public string ID_Poste { get; set; } 
     public int Position { get; set; } 
     public int Nbr_Passage { get; set; } 
     public string Last_Posts { get; set; } 
     public string Next_Posts { get; set; } 

     public virtual Poste Poste { get; set; } 
     public virtual Profile_Ga Profile_Ga { get; set; } 

    } 
} 
+0

我沒有看到視圖模型的Gamme和FlowViewModel之間共同的東西在這種情況下,。使用相同的視圖是不正確的,如果他們有共同的東西將其提取到基本視圖模型併爲該基本視圖模型編寫視圖,而不是復古適合。 – Sruti

+0

@Sruti thx ,,但這是一個複雜的問題, ,這裏的專家誰建議使用它來解決另一個問題,所以讓我們忘了它,,,但是有解決方案在視圖中使用它們嗎? – anouar

回答

1

替換該「<%:Html.EditorFor(X => x.YourGammeMod (「Nbr_Passage」,(Model!= null)& & Model.YourGammeModel!= null)? Model.YourGammeModel.Nbr_Passage:0)%>」

,並在後期的方法,你包括 例如

[HttpPost] 
public ActionResult Save (FlowViewModel flowViewModel, FromCollection form) 
{ 
//get value using formcollection 
int Nbr_Passage = (int)form["Nbr_Passage"]; 

}