2013-05-14 65 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