为什么我为JobSeekerId获得null错误,我通过参数传递所有强制值,但context.savechanges()返回错误
不能在表的JobseekerId列中插入NULL值"Sample1.dbo.JobseekerBackgroundDetail";列不允许为空。插入失败。语句已被终止。
但我确信我正在传递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,所有的值都是空的。请帮助。这个问题对我来说太大了,找不到问题
使用以下代码://添加(optinioal)。
public ActionResult actionname(datatype yourclassname)
{
db.Entry(yourclassname).State = System.Data.Entity.EntityState.Added;
db.savechanges();
}
并检查你的MVC视图发送所有类参数你的动作