Reputation: 317
I am developing an ASP.Net MVC 3 application using C# and SQL Server 2005. I am using Entity Framework with a code-first approach.
I have a model Profile_Ga with it Views(Index,Create,...).
I created a DropDownList in the Index of this model to load the IDs (ID_Gamme).
I want now to load the IDs of another table (another model which is Poste) in the Index of Profil_Ga.
But always this error appears :
DataBinding: 'MvcApplication2.Models.Profile_Ga' does not contain a property with the name 'ID_Poste'.
This is the Controller of Profile_Ga :
namespace MvcApplication2.Controllers
{
public class ProfileGaController : Controller
{
private GammeContext db = new GammeContext();
//
// GET: /ProfileGa/
public ViewResult Index(Profile_Ga profile_ga, Poste poste)
{
ViewBag.ID_Gamme = new SelectList(db.Profil_Gas, "ID_Gamme", profile_ga.ID_Gamme);
ViewBag.ID_Poste = new SelectList(db.Postes, "ID_Poste", poste.ID_Poste);
return View(db.Profil_Gas.ToList());
}
and this is what I add in the Index :
<%:Html.Label("Gamme :")%>
<%: Html.DropDownList("ID_Gamme", new SelectList(Model, "ID_Gamme", "ID_Gamme ")) %>
<%:Html.Label("Poste :")%>
<%: Html.DropDownList("ID_Poste", new SelectList(Model, "ID_Poste", "ID_Poste ")) %>
Upvotes: 0
Views: 673
Reputation: 8147
The error is spot on. The Model doesn't contain ID_Poste
.
You are storing db.Postes
into the ViewBag but the model being passed to the view is just db.Profil_Gas
as in this part: return View(db.Profil_Gas.ToList());
- this won't contain db.Postes
.
As you want to show two separate things the best approach would be to create a new ViewModel class that contains both things like this.
View Model
public class MyViewModel
{
// The two drop-down lists
public List<Profile_Ga> Profile_Gas { get; set; }
public List<Poste> Postes { get; set; }
// Used to store selected items
public int SelectedProfile_Ga { get; set; }
public int SelectedPoste { get; set; }
}
Then in your Controller
[HttpGet]
public ActionResult Index(Profile_Ga profile_ga, Poste poste)
{
var viewModel = new MyViewModel();
viewModel.Profile_Gas = db.Profil_Gas.ToList();
viewModel.Postes = db.Postes.ToList();
return View(viewModel);
}
[HttpPost]
public ActionResult Index(MyViewModel viewModel)
{
string debug = string.Format("You selected Profile: {0} and Poste: {1}", viewModel.SelectedProfile_Ga, viewModel.SelectedPoste);
return View(viewModel);
}
Finally in your View
<%: Html.DropDownList("SelectedProfile_Ga", new SelectList(Model.Profile_Gas, "ID_Gamme", "NameToShow")) %>
<%: Html.DropDownList("SelectedPoste", new SelectList(Model.Postes, "ID_Poste", "NameToShow")) %>
Then you simply replace NameToShow
with the property you want to show in the drop-down box. Then, when you submit the form the ViewModel will be passed back with the value of the drop down boxes (as shown in code example). Place breakpoint on debug in HttpPost
item to check the values are right and then you should be good to go!
Upvotes: 2
Reputation: 12956
Your view model is Profile_Gas
so you cannot do
<%:Html.Label("Poste :")%>
<%: Html.DropDownList("ID_Poste", new SelectList(Model, "ID_Poste", "ID_Poste ")) %>
You need to create a view model that encapsulates both Profile_Ga
and Poste
public class ViewModel
{
public List<Poste> Postes { get; set; }
public List<Profile_Ga> Profile_Gas { get; set; }
}
and return this view model from your controller.
public ViewResult Index(Profile_Ga profile_ga, Poste poste)
{
return new ViewModel
{
Postes = db.Postes.ToList(),
Profile_Gas = db.Profil_Gas.ToList();
}
}
So your view will look something like
<%:Html.Label("Gamme :")%>
<%: Html.DropDownList("ID_Gamme", new SelectList(Model.Profile_Gas, "ID_Gamme", "ID_Gamme ")) %>
<%:Html.Label("Poste :")%>
<%: Html.DropDownList("ID_Poste", new SelectList(Model.Postes, "ID_Poste", "ID_Poste ")) %>
Upvotes: 1