anouar
anouar

Reputation: 317

How to inherits differents models in the same view in ASP MVC

I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.

I am using also Entity Framework and Code First Method.

I have a partial View 'Gestion.ascx' which contains a Form (textbox and listbox).

This view use a ViewModel 'FlowViewModel'.

I want that this the view use another model 'Gamme' which i already have.

I try it to put them both in the 'inherits markup' but some statements became underlined in RED.

Infact, to explain more the question :

I used the FlowViewModel in this partial view in order to load some data in my list boxfrom differents models.

Now I want to store values selected and entred in a local variables.

I can't pass from the model 'Gamme' to the controller because the view 'Gestion' is not using the model 'Gamme'.

This is the code of the partial view '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>
<% } %>

and this is the code of the model '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; }
}
}

and this is the model '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; }

    }
}

Upvotes: 0

Views: 179

Answers (1)

sathishkumar
sathishkumar

Reputation: 11

Replace this "<%: Html.EditorFor(x=>x.YourGammeModel.Nbr_Passage)%>" code with "<%: Html.Textbox("Nbr_Passage",(Model != null && Model.YourGammeModel != null) ? Model.YourGammeModel.Nbr_Passage : 0) %>"

And in Post method you include for example

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

}

Upvotes: 1

Related Questions