0
为什么我得到空误差JobSeekerId,我已经通过了所有的强制值通过参数,但context.savechanges()返回我的错误context.savechanges()给错误而插入
无法插入值NULL到'JobseekerId'列中,表 'Sample1.dbo.JobseekerBackgroundDetail';列不允许有空值。 INSERT失败。该语句已终止。
但我相信我通过jobseekerid,我在调试时得到这个值。我想模型有一些问题。任何人都可以指出我的错误我在哪里做wrong.here出来是我的代码 型号:
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之外可以为空。请帮忙。卡住了这个问题,这对我来说很难找到问题..
不相关,但删除您的发布方法中的所有参数,并用'JobseekerBackgroundDetail模型'替换。 –
在IF检查你得到Id为:int id = Convert.ToInt32(Session [“LogedUserID”]); ,我不确定,你从ELSE部分获取Id的位置。 –
@Stephen Muecke,先生,我已经做到了,但找不到任何区别。仍然得到相同的错误,否则部分工作更新,但不能工作,如果加入的一部分 – Miranda