【问题标题】:How to inherits differents models in the same view in ASP MVC如何在 ASP MVC 中的同一视图中继承不同的模型
【发布时间】:2013-05-19 01:17:44
【问题描述】:

我正在使用 C# 和 SQL Server 2005 开发一个 ASP .Net MVC 3 应用程序。

我也在使用实体框架和代码优先方法。

我有一个包含表单(文本框和列表框)的部分视图“Gestion.ascx”。

此视图使用 ViewModel 'FlowViewModel'。

我希望这个视图使用我已经拥有的另一个模型“Gamme”。

我尝试将它们都放在“继承标记”中但有些语句在 RED 中加了下划线。

事实上,为了解释更多问题:

我在这个局部视图中使用了 FlowViewModel,以便在我的列表框中从不同的模型加载一些数据。

现在我想将选择的值存储在局部变量中。

我无法从模型“Gamme”传递到控制器,因为视图“Gestion”没有使用模型“Gamme”。

这是局部视图“Gestion”的代码:

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

    }
}

【问题讨论】:

  • 我看不出视图模型 Gamme 和 FlowViewModel 之间有什么共同点。在这种情况下,使用相同的视图是不正确的。如果他们有共同点,将其提取到基本视图模型并为该基本视图模型编写视图,而不是改造。
  • @Sruti thx,,但这是另一个复杂的问题,,这里的专家建议用它来解决另一个问题,,,所以让我们忘记它吧,,,但是有没有解决方案可以同时使用它们风景?

标签: c# sql-server visual-studio-2010 asp.net-mvc-3


【解决方案1】:

将此“x.YourGammeModel.Nbr_Passage)%>”代码替换为 ""

在 Post 方法中,您包括 例如

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 2011-06-08
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多