Instead of using add() in controller to add records, I want to create a service and create an add method in it but I'm getting a null reference error in if (addsolution == null) part . What am I missing?
Controller:
[HttpPost]
public ActionResult AddSolution(HataCozumViewModel solution)
{
var result = servis.AddSolution(solution);
return RedirectToAction("Index");
}
Service:
public HataCozumViewModel AddSolution(HataCozumViewModel psolution)
{
DBEntities1 hataprojedb = new DBEntities1();
HataCozum addsolution= hataprojedb.HataCozum.AsQueryable().FirstOrDefault();
if (addsolution== null)
{
addsolution.ID= psolution.ID;
addsolution.HataID = psolution.HataID;
addsolution.CozulenHataAdi = psolution.CozulenHataAdi;
addsolution.CozumAciklama = psolution.CozumAciklama;
addsolution.HatayiCozenKullaniciID = user.ID;
hataprojedb.HataCozum.Add(addsolution);
hataprojedb.SaveChanges();
}
return psolution;
}
View
<form class="form-group" method="post" action="\Home\AddSolution">
<div>
<label>Who's solving the error?</label>
@Html.DropDownListFor(x => x.HatayiCozenKullaniciID, (List<SelectListItem>)ViewBag.hatayicozenkisi1, new { @class = "form-control" })
</div>
<div>
<label>Error Explanation</label>
@Html.TextBoxFor(x => x.CozumAciklama)
</div>
<div>
<label>Error ID</label>
@Html.TextBoxFor(x => x.HataID)
</div>
<div>
<label>Error's name</label>
@Html.DropDownListFor(x => x.CozulenHataAdi, (List<SelectListItem>)ViewBag.hatalist1, new { @class = "form-control" })
</div>
<button type="submit">Save Solution</button>
</form>

servisdetails and how you are calling theservison thecontrollerclass? Are you getting data onActionResult AddSolution(HataCozumViewModel solution)?