0

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>
4
  • 1
    Could you please your full servis details and how you are calling the servis on the controller class? Are you getting data on ActionResult AddSolution(HataCozumViewModel solution)? Commented Apr 25, 2022 at 16:21
  • Hello does it resolve your issue? Do you need any further assistance on this? Commented Apr 26, 2022 at 6:45
  • I solved it and added my solution , thank you Commented Apr 26, 2022 at 6:58
  • Outstanding solution. Keep it up. Commented Apr 26, 2022 at 7:15

2 Answers 2

1

"What am I missing?"

Your form submit format is not correct please try to modify your snippet like below

View Should Be:

    <form class="form-group" method="post" asp-controller="YourControllerName" asp-action="YourActionName">
        <div>
            <label>CozulenHata Adi</label>
            @Html.TextBoxFor(x => x.CozulenHataAdi)
        </div>
        <button type="submit">Save Solution</button>
    </form>

Output:

enter image description here

Note: action="\Home\AddSolution"> this is not the valid way to write controller action name this eventually casuing your form submit data loss. Rather you should rewrite that like below:

<form class="form-group" method="post" asp-controller="YourControllerName" asp-action="YourActionName">

Hope that would resolve your issue and guided you accordingly.

Sign up to request clarification or add additional context in comments.

Comments

0

I solved this problem by changing the method like this and now it works. Thank you all for your help

  public HataCozumViewModel AddSolution(HataCozumViewModel psolution))
        {

            DBEntities1 hataprojedb = new DBEntities1();
            
            var addsolution= = hataprojedb.HataCozum.AsQueryable().Where(w=>w.ID == psolution.ID).FirstOrDefault();


            if (addsolution== null)
            {
                HataCozum test = new HataCozum();
                
                test.CozulenHataAdi = psolution.CozulenHataAdi;
                test.CozumAciklama = psolution.CozumAciklama;
                test.HatayiCozenKullaniciID = psolution.HatayiCozenKullaniciID;
                hataprojedb.HataCozum.Add(test);
                hataprojedb.SaveChanges();

            }
            return psolution;

1 Comment

Is there anything else that I can help you with?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.