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