实体在Post动作-MVC上返回null

时间:2017-05-04 10:29:45

标签: asp.net-mvc dapper

我对编程很新,我遇到了一些我无法弄清楚的问题。当我更新项目时,我在HttpPost返回一个null项目。 这是我的ProjectRepository代码:

 public bool UpdateProject(ProjectDetailsViewModel project)
        {
            try
            {
                using (var connection = DbAdapter.GetConnection())
                {
                    connection.Open();
                    var sql = @" UPDATE p_Project 
                                  SET 
                                     p_Project.ProjectName = @ProjectName
                                    ,p_Project.Client_ID = Client_ID
                                    ,p_Project.Industry] = @Industry
                                    ,p_Project.SubIndustry = @SubIndustry
                                    ,p_Project.ProjectType = @ProjectType
                                    ,p_Project.ProjectStatus = @ProjectStatus
                                    ,p_Project.EffectiveDate = @EffectiveDate
                                    ,p_Project.ExpirationDate = @ExpirationDate
                                    ,p_Project.DirectoryStructureType = @DirectoryStructureType
                                    ,p_Project.ContinuousProject = @ContinuousProject
                                    ,p_Project.ExpectedVolume = @ExpectedVolume
                                    ,p_Project.Warranty = @Warranty
                                    ,p_Project.ProjDateClosed = @ProjDateClosed
                                    ,p_Project.PricingDescription= @PricingDescription
                                    ,p_Project.ProjDescription = @ProjDescription
                                    ,p_Project.ActualRevenue = @ActualRevenue
                                    ,p_Project.LaterRevenue = @LaterRevenue
                                    ,p_Project.TotalForcastedRevenue = @TotalForcastedRevenue
                                    ,p_Project.MasterProject = @MasterProject
                                    ,p_Project.OwningProjectID = @OwningProjectId
                                    ,p_Project.MasterStatus = @MasterProjectStatus
                                    ,p_Project.ProjectOrigination = @ProjectOrigination
                                    ,p_Project.PrimaryMaterial = @PrimaryMaterial
                                    ,p_Project.PrimaryService =@PrimaryService
                                    ,p_Project.,PrimarySource =@PrimarySource
                                    ,p_Project.PrimaryTarget =@PrimaryTarget
                                    ,p_Project.ProjectCategory =@ProjectCategory
                               WHERE Project_ID=@Id";

                    connection.Execute(sql, new
                    {
                        project.Project.ProjectName
                       ,project.Project.ClientId
                       ,project.Project.Industry
                       ,project.Project.SubIndustry
                       ,project.Project.ProjectType
                       ,project.Project.ProjectStatus
                       ,project.Project.EffectiveDate
                       ,project.Project.ExpirationDate
                       ,project.Project.DirectoryStructureType
                       ,project.Project.ContinuousProject
                       ,project.Project.ExpectedVolume
                       ,project.Project.Warranty
                       ,project.Project.ProjDateClosed
                       ,project.Project.PricingDescription
                       ,project.Project.ProjDescription
                       ,project.Project.ActualRevenue
                       ,project.Project.LaterRevenue
                       ,project.Project.TotalForcastedRevenue
                       ,project.Project.MasterProject
                       ,project.Project.OwningProjectId
                       ,project.Project.MasterProjectStatus
                       ,project.Project.ProjectOrigination
                       ,project.Project.PrimaryMaterial
                       ,project.Project.PrimaryService
                       ,project.Project.PrimarySource
                       ,project.Project.PrimaryTarget
                       ,project.Project.ProjectCategory
                       ,project.Project.Id
                    });

                    connection.Close();

                }
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }

这是我的ProjectController代码:

 public ActionResult GetProjectView(ProjectDetailsViewModel project)
        {
            if (ModelState.IsValid)
            {
                projectDataAccess = new ProjectDataAccess();
                projectDataAccess.UpdateProject(project);

                ViewBag.Message = @"Project Updated Successfully";
                return View("Partial/ProjectDetails", project);
            }
            return RedirectToAction("Index");
        }
    }

这是我的ProjectDetailsView的一部分:

@model DCMS.Models.Projects.DataViewModels.ProjectDetailsViewModel
@{
    ViewBag.Title = "Project Details";
}

@using (Html.BeginForm("GetProjectView","Project"))
{
@*<div class="errormessage">@Html.ValidationSummary(false)</div>*@
if (ViewBag.Message != null)
    {
        <div class="InfoMessageOuter">
            <div class="InfoMessageMiddle">
                <div class="InfoMessageInner">@ViewBag.Message</div>
            </div>
        </div>
    }

<br/>
<h2 style="font-size: 20px; margin: 5px;">@Model.Project.Client.ClientName </h2>

<div>
<table style="margin: 0 30px;">

@Html.HiddenFor(m => m.Project.ClientId)
@Html.HiddenFor(m => m.Project.Id)
@Html.HiddenFor(m => m.Project.ModeledOn)


<tr class="control-evenrow">
    <td class="control-label">
        @Html.LabelFor(m => m.Project.ProjectName, new {@class = "dcl-control-label required"})
    </td>
    <td class="spacer-column"></td>
    <td class="control-input">
        @Html.TextBoxFor(m => m.Project.ProjectName, new {@class = "dcl-control-input"})
    </td>
     <td class="control-label">
        @Html.LabelFor(m => m.Project.ProjectNum, new {@class = "dcl-control-label required"})
    </td>
    <td class="spacer-column"></td>
    <td class="control-input">
        @Html.TextBoxFor(m => m.Project.ProjectNum, new {@readonly = "readonly", @class = "dcl-control-input"})
    </td>

这是我的ProjectDetailsViewModel:

 public class ProjectDetailsViewModel
    {
        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<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;
            }
        }
    }

这是我的提交按钮: <form action="" method="post"> <button id="projectDetailsSubmit" type="submit" name="cmd" rows="15" value="Edit" class="">Save</button> </form> 这是我的提交按钮,我也有:BeginForm(&#34; action&#34;,&#34; controller&#34;,FormMethod.Post)。我正在做一个简单的帖子,而不是使用AJAX。

0 个答案:

没有答案