0

我正在使用 C# 和 SQL Server 2005 开发一个 ASP.Net MVC 3 应用程序。我正在使用具有代码优先方法的实体框架。

我有一个带有 Views(Index,Create,...) 的模型 Profile_Ga。

我在该模型的索引中创建了一个 DropDownList 来加载 ID (ID_Gamme)。

我现在想在 Profil_Ga 的索引中加载另一个表(另一个模型是 Poste)的 ID。

但总是出现这个错误:

DataBinding:“MvcApplication2.Models.Profile_Ga”不包含名为“ID_Poste”的属性。

这是 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());


        }

这就是我在索引中添加的内容:

<%: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 ")) %>
4

2 回答 2

2

错误很明显。该模型不包含ID_Poste.

您正在存储db.Postes到 ViewBag 中,但传递给视图的模型就像db.Profil_Gas这部分一样:return View(db.Profil_Gas.ToList());- 这不会包含db.Postes.

当你想展示两个不同的东西时,最好的方法是创建一个包含这两个东西的新ViewModel类。

查看模型

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

然后在你的控制器中

[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);
}

终于在你的视野中

<%: Html.DropDownList("SelectedProfile_Ga", new SelectList(Model.Profile_Gas, "ID_Gamme", "NameToShow")) %>
<%: Html.DropDownList("SelectedPoste", new SelectList(Model.Postes, "ID_Poste", "NameToShow")) %>

然后,您只需替换NameToShow为要在下拉框中显示的属性。然后,当您提交表单时,ViewModel将与下拉框的值一起传回(如代码示例所示)。在项目中的调试上放置断点HttpPost以检查值是否正确,然后您应该一切顺利!

于 2013-05-10T10:30:39.873 回答
1

你的视图模型是Profile_Gas你不能做的

<%:Html.Label("Poste :")%>
       <%: Html.DropDownList("ID_Poste", new SelectList(Model, "ID_Poste", "ID_Poste ")) %>

您需要创建一个视图模型来封装Profile_GaPoste

public class ViewModel
{
    public List<Poste> Postes { get; set; }
    public List<Profile_Ga> Profile_Gas { get; set; }
}

并从您的控制器返回此视图模型。

public ViewResult Index(Profile_Ga profile_ga, Poste poste)
{
    return new ViewModel
            {
                 Postes = db.Postes.ToList(),
                 Profile_Gas = db.Profil_Gas.ToList();
            }
}

所以你的观点看起来像

<%: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 ")) %>
于 2013-05-10T10:32:19.497 回答