尝试更新数据库条目,但添加了一个新的 [英] Trying to update a database entry but a new one is added

查看:114
本文介绍了尝试更新数据库条目,但添加了一个新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里是短期运行:
我的系统有两种类型的用户:公司和学生。
公司发布工作,学生申请(将新条目添加到应用程序数据库表中),公司选择应用程序并将该作业提供给学生,该站点将他转到合同页面,其中公司可以看到合同的样子和学生的CPR(SSN)将去哪里。他或她填写公司的CVR(公司SSN)及其姓名,并点击提交。这将在合同数据库表中创建一个新条目。到目前为止很好。

So here's the short run down: My system has two types of users: company and student. The company posts a job, the student applies (a new entry is added to the application database table), the company selects the application and offers the job to the student, the site takes him to the contract page, where the company can see how the contract looks like and where the student's CPR (SSN) would go. He or she fills out the company's CVR (company SSN) and their first name and last name and clicks submit. This creates a new entry in the contract database table. So far so good.

这里是Contract.cs模型类:

Here is the Contract.cs model class:

namespace Leepio.Models
{
    public class Contract { 
        public int ContractId { get; set; }
        public string StudentId { get; set; }
        public string CompanyId { get; set; }
        public string CVR { get; set; }
        public int ApplicationId { get; set; }
        public bool IsSigned { get; set; }
        public string StudentSigningDate { get; set; }
        public string CompanySigningDate { get; set; }
        public string RepFirstName { get; set; }
        public string RepLastName { get; set; }
        public virtual ApplicationUser Student { get; set; }
        public virtual ApplicationUser Company { get; set; }
        public virtual Application Application { get; set; }


    }

两个字段:IsSigned字段(在学生接受和提交时应转为true)和StudentSigningDate,它只是学生接受的日期字符串形式的日期。
现在学生收到一封电子邮件,他或她去链接,他们可以看到合同的预览和CPR键入的文本框,然后提交按钮。

Everything is filled at this point except two fields: the IsSigned field (should turn to true when the student accepts and submits) and StudentSigningDate, which is just the date in form of a string of the day the student accepts). Now the student gets an email and he or she goes to the link where they can see a preview of the contract and a textbox where the CPR is typed in and then the submit button.

这是问题开始的地方。

这是提交按钮的方法:

 public ActionResult SendMailAsAStudent(string studentId, string companyId, int applicationId, string companyCVR, string studentCPR)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var student = manager.FindById(studentId);
            var company = manager.FindById(companyId);
            var application = db.Applications.Find(applicationId);
            var contractobj = db.Contracts.Find(applicationId);
            Project projectobj = db.Projects.Find(application.ProjectId);


            var myMessage = new SendGridMessage();
            myMessage.From = new MailAddress("info@leepio.dk");
            myMessage.AddTo(student.Email);
            myMessage.AddTo(company.Email);
            myMessage.Subject ="The contract for " + projectobj.Title + " is signed!";
            myMessage.Html = "Here is the final contract";


            LocalReport localReport = new LocalReport();

            localReport.ReportPath = Server.MapPath("~/Contracts/Contract.rdlc");
            ReportParameter cvrParameter = new ReportParameter("cvrParameter", companyCVR);
            ReportParameter companyNameParameter = new ReportParameter("companyNameParameter", company.CompanyName);
            ReportParameter companyAddressParameter = new ReportParameter("companyAddressParameter", company.Address);
            ReportParameter companyCityParameter = new ReportParameter("companyCityParameter", company.City);
            ReportParameter studentCityParameter = new ReportParameter("studentCityParameter", student.City);
            ReportParameter studentNameParameter = new ReportParameter("studentNameParameter", student.FirstName+" "+student.LastName);
            ReportParameter studentAddressParameter = new ReportParameter("studentAddressParameter", student.Address);
            ReportParameter studentZipCodeParameter = new ReportParameter("studentZipCodeParameter", student.ZipCode);
            ReportParameter jobStartDateParameter = new ReportParameter("jobStartDateParameter", projectobj.StartDate);
            ReportParameter jobEndDateParameter = new ReportParameter("jobEndDateParameter", projectobj.EndDate);
            ReportParameter jobDescriptionParameter = new ReportParameter("jobDescriptionParameter", projectobj.Description);
            ReportParameter jobHoursPerWeekParameter = new ReportParameter("jobHoursPerWeekParameter", projectobj.HoursPerWeek.ToString());
            ReportParameter jobHourlyRateParameter = new ReportParameter("jobHourlyRateParameter", projectobj.HourlyRate.ToString());
            ReportParameter cprParameter = new ReportParameter("cprParameter", studentCPR);
            ReportParameter studentSignDateParameter = new ReportParameter("studentSignDateParameter", DateTime.Today.ToString("dd/MM/yyyy"));
            ReportParameter companySignDateParameter = new ReportParameter("companySignDateParameter", contractobj.CompanySigningDate);
            ReportParameter companyRepNameParameter = new ReportParameter("companyRepNameParameter", contractobj.RepFirstName +" "+contractobj.RepLastName);
            ReportParameter projectWorkFromParameter = new ReportParameter("projectWorkFromParameter", projectobj.WorkFrom);


            localReport.SetParameters(new ReportParameter[] { cprParameter });
            localReport.SetParameters(new ReportParameter[] { cvrParameter });
            localReport.SetParameters(new ReportParameter[] { companyNameParameter });
            localReport.SetParameters(new ReportParameter[] { companyAddressParameter });
            localReport.SetParameters(new ReportParameter[] { studentNameParameter });
            localReport.SetParameters(new ReportParameter[] { studentAddressParameter });
            localReport.SetParameters(new ReportParameter[] { studentZipCodeParameter });
            localReport.SetParameters(new ReportParameter[] { jobStartDateParameter });
            localReport.SetParameters(new ReportParameter[] { jobEndDateParameter });
            localReport.SetParameters(new ReportParameter[] { jobDescriptionParameter });
            localReport.SetParameters(new ReportParameter[] { jobHoursPerWeekParameter });
            localReport.SetParameters(new ReportParameter[] { jobHourlyRateParameter });
            localReport.SetParameters(new ReportParameter[] { studentSignDateParameter });
            localReport.SetParameters(new ReportParameter[] { companySignDateParameter });
            localReport.SetParameters(new ReportParameter[] { companyRepNameParameter });
            localReport.SetParameters(new ReportParameter[] { projectWorkFromParameter });
            localReport.SetParameters(new ReportParameter[] { companyCityParameter });
            localReport.SetParameters(new ReportParameter[] { studentCityParameter });

            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension = "pdf";
            Warning[] warnings;
            string[] streams;
            var renderedBytes = localReport.Render(reportType, "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
            Response.AddHeader("content-disposition", "attatchment; filename=Contract." + fileNameExtension);

           MemoryStream stream = new MemoryStream(renderedBytes);
           myMessage.AddAttachment(stream, "Contract.pdf");


            var apiKey = "removed for this post";
            var transportWeb = new Web(apiKey);
            transportWeb.DeliverAsync(myMessage);

            Contract contract = db.Contracts.Find(contractobj.ContractId);

            contract.IsSigned = true;
            contract.StudentSigningDate = DateTime.Today.ToString("dd/MM/yyyy");



            //db.Contracts.Add(contract);
            db.SaveChanges();
            return RedirectToAction("Index", "Projects");
        }

然后会发生这种情况:

该条目添加为我想要的:isSigned为false,学生签名日期不是
当它被填充时,它在数据库中创建另一个条目,公司的签名日期,名字和姓氏为false!

The entry is added as I wanted it to: isSigned is false and the student signing date is not yet filled in. When it does get filled in, it creates another entry in the database, with the company's signing date, first name and last name to false!

是什么原因?我注释掉了db.Contracs.Add(合同),我找到了我需要的applicationId,因为那将是唯一的标识符。我究竟做错了什么?我想要能够添加学生签名日期,并将IsSigned bool更改为true。
请注意,学生CPR不存储在数据库中,20是应用程序的ID

What causes this? I've commented out the db.Contracs.Add(contract), I find the contract I needed by applicationId since that would be the unique identifier. What am I doing wrong? I want to be able to add the student sign in date and change the IsSigned bool to true. Note that the student CPR is not stored in the database, the "20" is the application's ID

推荐答案

我找到了解决方案:

我现在传递contractId作为参数,而不是我以前的所有:

I now pass the contractId as a parameter instead of all the ones I had before:

public ActionResult SendMailAsAStudent(int contractId, string studentCPR)

那么我只是检索合同如此:

then I just retrieve the contract as so:

Contract contract = db.Contracts.Find(contractId);

已更改参数的所有检索过程:

Changed all the retrieving process for the parameters:

 ReportParameter cvrParameter = new ReportParameter("cvrParameter", contract.CVR);
            ReportParameter companyNameParameter = new ReportParameter("companyNameParameter", contract.Company.CompanyName);
            ReportParameter companyAddressParameter = new ReportParameter("companyAddressParameter", contract.Company.Address);
            ReportParameter companyCityParameter = new ReportParameter("companyCityParameter", contract.Company.City);
            ReportParameter studentCityParameter = new ReportParameter("studentCityParameter", contract.Student.City);
            ReportParameter studentNameParameter = new ReportParameter("studentNameParameter", contract.Student.FirstName+" "+ contract.Student.LastName);
            ReportParameter studentAddressParameter = new ReportParameter("studentAddressParameter", contract.Student.Address);
            ReportParameter studentZipCodeParameter = new ReportParameter("studentZipCodeParameter", contract.Student.ZipCode);
            ReportParameter jobStartDateParameter = new ReportParameter("jobStartDateParameter", contract.Application.Project.StartDate);
            ReportParameter jobEndDateParameter = new ReportParameter("jobEndDateParameter", contract.Application.Project.EndDate);
            ReportParameter jobDescriptionParameter = new ReportParameter("jobDescriptionParameter", contract.Application.Project.Description);
            ReportParameter jobHoursPerWeekParameter = new ReportParameter("jobHoursPerWeekParameter", contract.Application.Project.HoursPerWeek.ToString());
            ReportParameter jobHourlyRateParameter = new ReportParameter("jobHourlyRateParameter", contract.Application.Project.HourlyRate.ToString());
            ReportParameter cprParameter = new ReportParameter("cprParameter", studentCPR);
            ReportParameter studentSignDateParameter = new ReportParameter("studentSignDateParameter", DateTime.Today.ToString("dd/MM/yyyy"));
            ReportParameter companySignDateParameter = new ReportParameter("companySignDateParameter", contract.CompanySigningDate);
            ReportParameter companyRepNameParameter = new ReportParameter("companyRepNameParameter", contract.RepFirstName +" "+contract.RepLastName);
            ReportParameter projectWorkFromParameter = new ReportParameter("projectWorkFromParameter", contract.Application.Project.WorkFrom);

这也固定了我的其他问题与参数从我的其他职位。
尝试了设置此报告中未定义的报告参数studentSignDateParameter

This also fixed my other problem with the parameter from my other post. An attempt was made to set a report parameter 'studentSignDateParameter' that is not defined in this report

这篇关于尝试更新数据库条目,但添加了一个新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆