MVC Passed List <t>从视图到控制器是NULL

时间:2017-10-19 11:21:46

标签: c# asp.net-mvc model-view-controller null

请你知道为什么我的List在从视图传递模型后是NULL。我尝试了我在这里找到的任何解决方案,但仍然没有。 List是具有另一个视图的模型类的一部分。

模型视图:

public class PorizeniWzView
{
    public PorizeniWzDataView ProjectDataView { get; set; }
    public PorizeniWzSuppliersView SupplierView { get; set; }
    public PorizeniWzProjectStateView ProjectStateView { get; set; }
    public WzUberView WzUberView { get; set; }

    public PorizeniWzEventView CurrentEventView { get; set; }
    public PorizeniWzEventView NextEventView { get; set; }

    public PorizeniWzPatternsView PatternView { get; set; }
    public PorizeniWzPhasesView CurrentPhaseView { get; set; }


    public List<PorizeniWzEventView> ListOfProjectEvents { get; set; }
    public List<int> ListOfProjectEventInt { get; set; }
    public List<PorizeniWzProjectStateView> ListOfProjectStates { get; set; }
}

查看: 在视图中我使用索引++的foreach循环。我有自己的html帮助器来输入datetime。它工作正常。在视图中我需要为BasicEvent设置一个日期,所以我只从列表中选择基本事件。有用。

@using (Html.BeginForm("a1_FillUpEventsSave", "PorizeniWZ", FormMethod.Post, new { })){

@Html.RowText1For(m => m.ProjectDataView.Project_Id, READ_ONLY, DEBUG)

int[] index = {-1};
foreach (var ev in listOfEvents)
{
    if (!ev.BasicEvent.HasValue || ev.BasicEvent == false) {
        index[0]++; continue;
    }
    index[0]++;
    <div class="row" style="margin-top: 15px;">

        <div title='Klíčová událost' class='btn btn-xs'>
            <div class='glyphicons glyphicons-keys'></div>
        </div>
        <b style="font-size: 17px">@ev.EventTypeNameCz</b> &nbsp;&nbsp; <span class="btn btn-xs" title="@ev.PhaseName" style="background-color: @ev.PhaseWebColor"><span class="@ev.PhaseWebIcon" style="color: white"></span></span>

        @Html.RowText1For(m => m.ListOfProjectEvents[index[0]].Event_Id, READ_ONLY, DEBUG)
        @Html.RowDate1For(m => m.ListOfProjectEvents[index[0]].DatePlanned, view)
    </div>      }
<div class="row">
    <div class="col-xs-12 text-center" style="margin-top: 30px;">
        <input type="submit" value="Potvrdit termíny u klíčových událostí" class="btn btn-success" style="width: 400px; font-weight: bold" />
    </div>
</div>} 

控制器: 有一个方法GetFullView(id)。此方法使用数据库中的信息填充视图。 ProjectEvents列表已初始化。

       public ActionResult a1_CreateFillUpEvents(int id)
    {
        var view = _repo.GetFullView(id);
        if (view == null) KontrolaHelp.Error("Projekt pořízení WZ nenalezen!");

        return View("CreateFillUpEvents", view);
    }

控制器(保存表单):在这里,我在数据库中添加事件

    [HttpPost]
    public ActionResult a1_FillUpEventsSave(PorizeniWzView view)
    {
        if(!ModelState.IsValid) KontrolaHelp.ErrorModel(ModelState);
        var t = _repo.GetPodlePK(view.ProjectDataView.Project_Id);            
        if (t == null) KontrolaHelp.Error("Chyba DB. Projekt pořízení WZ nenalezen!");
        using (var transakce = _jednotka.Transakce_New())
        {
            foreach (var ev in view.ListOfProjectEvents)
            {
                if (ev.BasicEvent.HasValue && ev.BasicEvent.Value)
                {
                    var e = t?.Events1.Single(x => x.Event_Id == ev.Event_Id);
                    if (e == null) continue;
                    e.DatePlanned = ev.DatePlanned;
                    _repoEvents.Update(e);
                }
            }

            t.ProjectState_Id = (int) PorizeniWzSTAV.a17_Nastaveny;
            _repo.Update(t);
            transakce.Commit();
        }

        return RedirectToAction("Index");
    }    

INIT事件列表的方法

        public List<PorizeniWzEventView> GetEventsForProject(int? id)
    {
        var repoEventTypes = new TabPorizeniWzEventsTypesRepo(_jednotkaPrace);
        var set = GetSet().Where(t => t.ProjectId == id).OrderBy(t => t.Order);
        var list = set.ToList();
        foreach (var porizeniWzEventView in list){
            porizeniWzEventView.ListOfStates = repoEventTypes.DoplnStavyProTypUdalosti(porizeniWzEventView.EventTypeId);
        }
        return set.ToList();
    }

0 个答案:

没有答案