簡體   English   中英

如何在ASP MVC 3的同一視圖中使用來自不同模型的參數

[英]How to use parametres from differents models in the same View in ASP MVC 3

我正在使用C#和SQL Server 2005開發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();

         }




    }

請在查看頁面中使用以下內容進行檢查

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

或者如果您只需要Gamme模型,則必須使用以下命令

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

您需要將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(){...}};

並使用flowviewmodel內部的gamme屬性:

<% Model.YourGammeModel.Nbr_Passage %>

您在此行中遇到的問題:

<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>" %>

根據您昨天發送給我的代碼,您正在將Gestion渲染為另一個視圖中的局部視圖,因此實際上並未執行此控制器操作。 您需要更改Index.aspx內容

Html.RenderPartial("Gestion")

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

並將控制器操作更改為此:

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

    return PartialView(model);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM