2

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 want to upload data from a table (from my base) in a DropDownList.

The table is 'Poste'and it has a model 'Poste'.

The view showing the DropDownList is 'Gestion' and this its code :

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.FlowViewModel>" %>

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset class="parametrage">
        <legend>Gestion de Gamme</legend>

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


         <div><%:Html.Label("Nombre de Passage :")%></div>
        <div><%:Html.Label("Position :")%></div>
        <div><%:Html.Label("Poste Précédente :")%></div>
        <div><%:Html.Label("Poste Suivante :")%></div>

        </fieldset>
         <% } %>

As you see, it is import from a FlowViewModel, which the code is :

namespace MvcApplication2.Models
{
    public class FlowViewModel
    {
        [Key]
        public string IDv { get; set; }
        public List<Poste> PostesItems { get; set; }
        public List<Profile_Ga> Profile_GaItems { get; set; }
        public List<Gamme> GaItems { get; set; }

        public int SelectedProfile_Ga { get; set; }

        public int SelectedGamme{ get; set; }

        public int SelectedPoste { get; set; }
    }
}

and this is the controller which returns the view :

public class ProfileGaController : Controller
    {
        private GammeContext db = new GammeContext();

        //
        // GET: /ProfileGa/
        [HttpGet]
        public ActionResult Index(Profile_Ga profile_ga, Poste poste)
        {

            var viewModel = new FlowViewModel();
            viewModel.PostesItems = db.Postes.ToList();
               viewModel.Profile_GaItems = db.Profil_Gas.ToList();
               viewModel.GaItems = db.Gammes.ToList();
               return View(viewModel);



        }

The problem that an Exception appears in the execution which mentionne that the values are passing NULL : error

4

3 回答 3

3

您可以为FlowViewModel和 intitialise创建一个构造函数PostesItems = new List<Poste>(),这样如果没有任何项目,它将是一个空列表而不是 null。

编辑,如果不清楚,请尝试更改FlowViewModel为 -

    public class FlowViewModel
    {
        public FlowViewModel()
        {
            PostesItems = new List<Poste>()
        }

        [Key]
        public string IDv { get; set; }
        public List<Poste> PostesItems { get; set; }
        public List<Profile_Ga> Profile_GaItems { get; set; }
        public List<Gamme> GaItems { get; set; }

        public int SelectedProfile_Ga { get; set; }

        public int SelectedGamme{ get; set; }

        public int SelectedPoste { get; set; }
    }    

再次编辑:在聊天中解决了这个问题。Index 操作正在调用一个运行良好的 Index 视图,但随后视图试图使用 Javascript 加载不同的视图,而不是传入模型,因此在屏幕截图中它是 null 的模型。

于 2013-05-13T14:09:27.417 回答
1

您的代码非常适合我。如果你改变会发生什么

viewModel.PostesItems = db.Postes.ToList();

viewModel.PostesItems = db.Postes.ToList() ?? new List<Poste>();

并且(可能对其他两个属性也这样做)?

(我很惊讶你必须这样做。我认为ToList返回的是一个空列表而不是 null。)

于 2013-05-13T14:29:12.420 回答
1

您在这里真正需要的是具有支持字段的属性。

 public class FlowViewModel
 {
    private List<Poste> _postesItems = null;
    public List<Poste> PostesItems
    {
       get
       {
          return _postesItems ?? new List<Poste>();
       }
       set
       {
          _postesItems = value ?? new List<Poste>();
       }
    }
 }

这解决了你所有的问题,你不再需要 viewModel 线了......

于 2013-05-13T15:16:08.727 回答