context.savechanges()在插入时给出错误

时间:2015-09-06 09:23:33

标签: c# asp.net-mvc savechanges

为什么我为JobSeekerId获取null错误,我已通过参数传递所有必需值但context.savechanges()返回错误

  

无法将值NULL插入列' JobseekerId',表中   ' Sample1.dbo.JobseekerBackgroundDetail&#39 ;;列不允许空值。   INSERT失败。声明已经终止。

但我确定我正在传递jobseekerid,我在调试时得到了这个值。我猜模型有一些问题。任何人都可以从我的错误中指出我做错了。我的代码也是 型号:

public class JobseekerBackgroundDetail
    {
        [Key]
        public int JobseekerId { get; set; }
        public string HighestDegree { get; set; }
        public string Specialisation { get; set; }
        public Nullable<int> PassingYear { get; set; }
        public Nullable<double> Percentage { get; set; }
        public string University { get; set; }
        public string Country { get; set; }
        public string TechnicalExp { get; set; }
        public string WorkField { get; set; }
    }

控制器

[HttpPost]
        public ActionResult JobSeekerAddEditEducation(string HighestDegree, string Specialisation, int PassingYear, double Percentage, string University, string Country, string TechnicalExp, string WorkField)
        {
            EmployeeContext employeeContext = new EmployeeContext();
            JobseekerBackgroundDetail jobseekerBackgroundDetail = new JobseekerBackgroundDetail();

            if (Session["LogedUserID"] != null)
            {
                int id = Convert.ToInt32(Session["LogedUserID"]);
                bool exists = employeeContext.JobseekerBackgroundDetails.Any(row => row.JobseekerId == id);
                if (exists != true)
                {
                    if (ModelState.IsValid)
                    {
                        jobseekerBackgroundDetail.JobseekerId = id;
                        jobseekerBackgroundDetail.HighestDegree = HighestDegree;
                        jobseekerBackgroundDetail.Specialisation = Specialisation;
                        jobseekerBackgroundDetail.PassingYear = PassingYear;
                        jobseekerBackgroundDetail.Percentage = Percentage;
                        jobseekerBackgroundDetail.University = University;
                        jobseekerBackgroundDetail.Country = Country;
                        jobseekerBackgroundDetail.TechnicalExp = TechnicalExp;
                        jobseekerBackgroundDetail.WorkField = WorkField;
                        employeeContext.JobseekerBackgroundDetails.Add(jobseekerBackgroundDetail);
                        employeeContext.SaveChanges();
                    }
                    return RedirectToAction("JobSeekerProfile");
                }
                else
                {
                    var query = from p in employeeContext.JobseekerBackgroundDetails
                                where p.JobseekerId == id
                                select p;
                    foreach (JobseekerBackgroundDetail p in query)
                    {
                        p.JobseekerId = id;
                        p.HighestDegree = HighestDegree;
                        p.Specialisation = Specialisation;
                        p.PassingYear = PassingYear;
                        p.Percentage = Percentage;
                        p.University = University;
                        p.Country = Country;
                        p.TechnicalExp = TechnicalExp;
                        p.WorkField = WorkField;
                    }
                    try
                    {
                        employeeContext.SaveChanges();
                    }
                    catch (Exception e)
                    {
                    }
                    return RedirectToAction("JobSeekerProfile");
                }
            }
            else
            {
                return RedirectToAction("JobSeekerLogin", "JobSeeker");
            }
        }

视图

@model Sample.Models.JobseekerBackgroundDetail
@using (Html.BeginForm("JobSeekerAddEditEducation", "JobSeeker", FormMethod.Post))
{
    <div class="container">

                        @Html.Label("Highest Degree", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.TextBoxFor(model => model.HighestDegree, new {  @class = "form-control", @placeholder = "HighestDegree" })


                        @Html.Label("Specialisation", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.TextBoxFor(model => model.Specialisation, new { @class = "form-control", @placeholder = "Specialisation" })

                        @Html.Label("Passing Year", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.TextBoxFor(model => model.PassingYear,  new { @class = "form-control", @placeholder = "Passing Year" } )


                        @Html.Label("Percentage", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.TextBoxFor(model => model.Percentage, new { @class = "form-control", @placeholder = "Percentage" })


                        @Html.Label("University", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.TextBoxFor(model => model.University, new { @class = "form-control", @placeholder = "University" })

                        @Html.Label("Country", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.TextBoxFor(model => model.Country, new { @class = "form-control", @placeholder = "Country" })                       


                        @Html.Label("Technical Exp", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.TextBoxFor(model => model.TechnicalExp, new { @class = "form-control", @placeholder = "Technical Exp" })


                        @Html.Label("Work Field", htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.TextBoxFor(model => model.WorkField, new { @class = "form-control", @placeholder = "Work Field" })

                    <button type="submit" class="btn btn-primary pull-left col-md-offset-2">Submit</button>

    </div>
}

在我的模型中,除了JobseekerId之外,所有值都可以为空。请帮忙。坚持这个问题对我来说很难找不到问题..

1 个答案:

答案 0 :(得分:0)

使用此代码: //添加(optinioal)。

&#13;
&#13;
public ActionResult actionname(datatype yourclassname)
{
db.Entry(yourclassname).State = System.Data.Entity.EntityState.Added;
db.savechanges();
}
&#13;
&#13;
&#13; 并检查你的mvc视图发送所有类参数你的行动。