![](/img/trans.png)
[英]How to use parametres from differents models in the same View in ASP MVC 3
[英]How to inherits differents models in the same view in ASP MVC
我正在使用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; }
}
}
用“ <%:Html.Textbox(” Nbr_Passage“,(Model!= null && Model.YourGammeModel!= null)替換此” <%:Html.EditorFor(x => x.YourGammeModel.Nbr_Passage)%>“代碼嗎? Model.YourGammeModel.Nbr_Passage:0)%>“
在Post方法中,例如
[HttpPost]
public ActionResult Save (FlowViewModel flowViewModel, FromCollection form)
{
//get value using formcollection
int Nbr_Passage = (int)form["Nbr_Passage"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.