C# 在ASP MVC中如何在同一视图中继承不同的模型
我正在使用C#和SQLServer2005开发一个ASP.NETMVC3应用程序 我还使用实体框架和代码优先方法 我有一个局部视图“Gestion.ascx”,其中包含一个表单(文本框和列表框) 此视图使用ViewModel“FlowViewModel” 我希望这个视图使用另一个我已经拥有的模型“Gamme” 我尝试将它们都放在“继承标记”中,但有些语句用红色下划线。 事实上,为了进一步解释这个问题: 我在这个局部视图中使用了FlowViewModel,以便从不同的模型加载列表框中的一些数据 现在,我想在局部变量中存储所选和输入的值 我无法从模型“Gamme”传递到控制器,因为视图“Gestion”未使用模型“Gamme”。 这是局部视图“手势”的代码:C# 在ASP MVC中如何在同一视图中继承不同的模型,c#,sql-server,visual-studio-2010,asp.net-mvc-3,C#,Sql Server,Visual Studio 2010,Asp.net Mvc 3,我正在使用C#和SQLServer2005开发一个ASP.NETMVC3应用程序 我还使用实体框架和代码优先方法 我有一个局部视图“Gestion.ascx”,其中包含一个表单(文本框和列表框) 此视图使用ViewModel“FlowViewModel” 我希望这个视图使用另一个我已经拥有的模型“Gamme” 我尝试将它们都放在“继承标记”中,但有些语句用红色下划线。 事实上,为了进一步解释这个问题: 我在这个局部视图中使用了FlowViewModel,以便从不同的模型加载列表框中的一些数据 现
<%@ 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>
<% } %>
将此“x.yourgamemodel.Nbr_Passage)%>”代码替换为
“”
在Post方法中,您包括
比如说
[HttpPost]
public ActionResult Save (FlowViewModel flowViewModel, FromCollection form)
{
//get value using formcollection
int Nbr_Passage = (int)form["Nbr_Passage"];
}
我看不到视图模型Gamme和FlowViewModel之间有任何共同之处。在这种情况下,使用相同的视图是不正确的。如果他们有共同点,则将其提取到基础视图模型中,并为该基础视图模型编写视图,而不是进行复古拟合。@Sruti-thx,,但这是一个非常复杂的问题,这里的专家建议使用它来解决另一个问题,,,所以,让我们忘掉它,,,但有解决方案可以在视图中同时使用它们吗?
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; }
}
}
[HttpPost]
public ActionResult Save (FlowViewModel flowViewModel, FromCollection form)
{
//get value using formcollection
int Nbr_Passage = (int)form["Nbr_Passage"];
}