2013-05-14 54 views
0

我使用C#和SQL Server 2005如何在ASP MVC 3

我也使用實體框架和代碼的方法首先開發一個ASP.NET的MVC 3應用程序中使用從型動物模型參數應用在同樣的觀點。

我有一個從模型視圖'FlowModelView'繼承的部分視圖,因爲我使用了這個模型中的列表。

我想使用另一個模型'Gamme'的參數。

當我嘗試這個,,,聲明總是在紅色下劃線。

這是局部視圖:

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

<% using (Html.BeginForm()) { %> 
    <%: Html.ValidationSummary(true) %> 
    <fieldset class="parametrage"> 
     <legend>Gestion de Gamme</legend> 

     <div><%:Html.Label("Poste :")%><%: Html.DropDownList("SelectedPoste", Model.PostesItems)%></div> 


     <div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(Model.Gamme=>Model.Gamme.Nbr_Passage) %></div> 

     </fieldset> 
     <% } %> 

這個FlowViewModel:

public class FlowViewModel 
    { 

     [Key] 
     public string IDv { get; set; } 
     [NotMapped] 
     public SelectList PostesItems { get; set; } 
     //public List<Poste> PostesItems { get; set; } 
     public List<Profile_Ga> Profile_GaItems { get; set; } 
     public List<Gamme> GaItems { get; set; } 

     public int SelectedProfile_Ga { get; set; } 

     public int SelectedGamme{ get; set; } 

     public int SelectedPoste { get; set; } 
    } 

,這是的Gamme型號:

namespace MvcApplication2.Models 
{ 
    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; } 

    } 
and this the controller but contains some errors : 


    public class AnouarController : Controller 
    { 



     // 
     // GET: /Anouar/ 

     public ActionResult Gestion() 
     { 
      var model = new FlowViewModel() 
      { YourGammeModel = new Gamme(){ 

     public string ID_Gamme { get; set; } 

     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; } 

      }}; 

      return PartialView(); 

     } 




    } 

回答

1

基礎的在你昨天發給我的代碼中,你將Gestion渲染爲另一個視圖中的一部分,所以這個控制器操作實際上並未被擊中。您需要更改Index.aspx那裏說

Html.RenderPartial("Gestion")

Html.RenderAction("Gestion", "Anouar", Model)

,改變控制器動作這樣:

public ActionResult Gestion(FlowViewModel model) 
{ 
    model.YourGammeModel = new Gamme(); 

    return PartialView(model); 
} 
+0

感謝MIXTE ,,,但是當我改變「的RenderPartial」 ,,,昨天的空例外返回 – anouar 2013-05-14 13:33:17

+0

OK我已經回到到該聊天室 – 2013-05-14 13:36:26

1

請通過使用在下面的檢查查看頁面

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

,或者如果你只需要Gamme模式,你必須使用以下

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

THX ,,,但我會怎麼稱呼在視圖中的pramateres:<%:Html.EditorFor(Model => Model.Nbr_Passage)%>我嘗試這個,但不工作 – anouar 2013-05-14 09:11:15

+0

作爲喬治指出,你可以嘗試一個Imports語句。你是否可以通過intellisense使用'Model.'來獲得Gamme的屬性。導入可以像這樣:<%@ Imports Namespace =「MvcApplication2.Models」%>'。 – Saravanan 2013-05-14 09:13:35

+0

我將使用2個模型,當我從模型中調用parametre時,他將如何知道哪個模型被調用? – anouar 2013-05-14 09:22:20

1

您需要添加一個成員的Gamme的FlowViewModel您可以在視圖傳遞,然後使用它。 只能將一個模型傳遞給視圖(如果需要,可以使用ViewBag傳遞其他數據)。 所以,也延長FlowViewModel或者您使用的的Gamme類廣告的模式,你的看法

@using MvcApplication2.Models.Gamme 

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; } 
    //here is the gamme member 
    public Gamme YourGammeModel {get;set;} 

    public int SelectedProfile_Ga { get; set; } 

    public int SelectedGamme{ get; set; } 

    public int SelectedPoste { get; set; } 
} 

你初始化模式

var model = new FlowViewModel(){ YourGammeModel = new Gamme(){...}}; 

,並使用流程模型內的gamme屬性:

<% Model.YourGammeModel.Nbr_Passage %> 
+0

THX但我不使用剃刀,,,我<使用%MvcApplication2.Models.Gamme%>試試這個,但沒有工作 – anouar 2013-05-14 09:13:09

+0

我編輯的迴應。同樣的概念也適用於非剃刀視圖。你必須選擇一個你想傳遞的模型,然後使用它。你可以像你想要的其他物體一樣嵌入到主模型中。在你的情況下,你將在flowviewmodel中添加一個Gamma成員。 – 2013-05-14 09:17:31

+0

我知道,,,但是我想用FlowViewModel,因爲我將在其他參數應用在以後使用它,我需要從型動物模型 – anouar 2013-05-14 09:21:12

1

你在這個線的問題:

<div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(Model.Gamme=>Model.Gamme.Nbr_Passage) %></div> 

你寫的lambda表達式Model.Gamme=>Model.Gamme.Nbr_Passage但應該寫g=>g.Nbr_Passage其中g是類型Gamme

並更改類型的視圖:

<%@ Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.Models.Ga‌​mme>" %> 
+0

THX ,,,但在這裏我將定義的Gamme – anouar 2013-05-14 09:14:53

+0

的實例摹正是這種partialview <%@ LANGUAGE =「C#」繼承=「System.Web.Mvc的模型。 ViewUserControl 「%> – 2013-05-14 11:06:00

+0

我嘗試過,但他不知道'Gamme'。這是錯誤:類型名稱「的Gamme」不存在類型存在「MvcApplication2.Models.FlowViewModel」 – anouar 2013-05-14 11:19:15