I am trying to save changes to a database using EntityFramework, however I get a null excpetion error, but I know the data is not null.
public partial class Model1
{
public int ID { get; set; }
public string Code { get; set; }
public bool Compulsary { get; set; }
public string Year { get; set; }
}
private Entity db = new Entity();
[HttpPost]
public ActionResult Add(string code)
{
HttpCookie cookie = Request.Cookies["Login"];
int ID = int.Parse(cookie["ID"]);
Model1 model1 = new Model1
{
ID = ID,
Code = code,
Compulsary = false,
Year = "P2"
};
db.Model1.Add(model1);
db.SaveChanges();
return View();
}
When it gets to db.SaveChanged then I get an error.
It looks like from here, the Code property could be at fault.
int ID = int.Parse(cookie["ID"]);
If the above line were at fault, it would fail on that line. The remaining properties are hard-coded, except for Code. You could be returning a null value from the post into a (possibly) non-nullable field in the database.
A possible cause is post variable naming. Check to make sure they are the same name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.