ListBoxFor在提交页面时返回空值

时间:2017-05-09 19:21:15

标签: asp.net-mvc

我遇到了ListBoxFor的问题...它可以通过Get请求正常工作,它会加载列表并预先选择所选的值但是当用户编辑表单并提交页面时,它会返回null ;它没有返回选定的值...任何人都可以帮忙吗?我对编程很新,我无法解决这个问题。 这是我的ProjectDetailsViewModel:

public class ProjectDetailsViewModel
    {
        public string ReturnMessage { get; set; }
        public Project Project { get; set; }
        public List<ProjSubIndustryMaster> SubIndustries { get; set; }
        public List<ProjIndustryMaster> Industries { get; set; }
        public List<Potential> Potentials { get; set; }
        public List<ProjectCategoryMaster> Categories { get; set; }
        public List<ProjectResources> ProjectManagers { get; set; }


        public List<ProjectResources> SalesManagers { get; set; }

        public List<ProjectOriginationMaster> OriginationList { get; set; }
        public List<MasterProject> MasterProjects { get; set; }
        public List<ProjectStatusMaster> ProjectStatusList { get; set; }
        public List<PrimaryMaterials> PrimaryMaterialList { get; set; }
        public List<ProjectMaterialMaster> MaterialList { get; set; }
        public List<PrimaryServices> PrimaryServiceList { get; set; }
        public List<ServiceCodeMaster> ServiceList { get; set; }
        public List<PrimarySources> PrimarySourceList { get; set; }
        public List<SourceCodeMaster> SourceList { get; set; }
        public List<ProjTechnologyTypeMaster> TechnologyTypesList { get; set; }
        public List<PrimaryTargets> PrimarytargetList { get; set; }
        public List<TargetCodeMaster> TargetCodeList { get; set; }
        public List<ProjectTypeMaster> ProjectTypesList { get; set; }
        public List<ProjectComment> Comments { get;set; }


        public List<string> SelectedServices
        {
            get
            {
                var serviceList1 = Project?.ProjectPricings?.Where(x => x.ProjectId == Project.Id && !string.IsNullOrWhiteSpace(x.ServiceCode))
                        .Select(x => x.ServiceCode)
                        .ToList();
                var serviceList2 = Project?.Services?.Select(x => x.ProjServiceCode).ToList();
                var serviceList = new List<string>();
                if (serviceList1 != null) serviceList.AddRange(serviceList1);
                if (serviceList2 != null) serviceList.AddRange(serviceList2);
                return serviceList;
            }
        }
    }
}

为了清楚起见:我在列表框中加载了从Master(查找)表中获取的数据,因此用户可以拥有&#34; all&#34;可供选择的选项,例如:ProjectMaterialMaster表,其中包含2列-Id和Material ...换句话说,所选值不是从与listBox中的数据相同的表中获取的。表格如下所示:

    ProjectMaterialMasterId  MaterialCode
      1                          A
      2                          C
    ...
     ProjectMaterialId      ProjectId     MaterialCode
        1                     4118            C
        2                     5342            C
        3                     5647            A
        4                     4118            B
 ...

第一个表格中包含所有可用材料,第二个表格是一个映射表格,将所有项目连接到相关材料......

这是我的观点:

<td class="control-input">
            @{
                var materials = Model.Project?.Materials.Select(x=>x.ProjMaterialCode);
            }
            @Html.ListBoxFor(m => materials, new MultiSelectList(Model.MaterialList, "ProjMaterialCode", "ProjMaterialCode", materials), new {@class = "listbox"})
        </td>

这是控制器:

[HttpPost]
        public ActionResult UpdateProjectView( ProjectDetailsViewModel projectViewModel)
        {

            if (!ModelState.IsValid)
            {

                projectViewModel.ReturnMessage = @"Error! Update Failed.";
                return GetProjectView(projectViewModel.ReturnMessage, projectViewModel.Project.Id);
            }
            if (projectViewModel.Project.Id != null)
            {
                var success = ProjectDataAccess.UpdateProject(projectViewModel.Project);
                projectViewModel.ReturnMessage = success ? @"Project Updated Successfully" : @"Error! Update Failed";
            }
            else if(projectViewModel.Project.Id == null)
            {
                var success = ProjectDataAccess.AddProject(projectViewModel.Project);
                projectViewModel.ReturnMessage = success ? @"Project Added Successfully" : @"Error! Insert Failed";
            }
            return GetProjectView(projectViewModel.ReturnMessage, projectViewModel.Project.Id);
        }

1 个答案:

答案 0 :(得分:0)

m => materials

应该是

m => m.materials