Home > database >  How to create add method in MVC?
How to create add method in MVC?

Time:04-26

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  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>

CodePudding user response:

"What am I missing?"

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

View Should Be:

    <form  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  method="post" asp-controller="YourControllerName" asp-action="YourActionName">

Hope that would resolve your issue and guided you accordingly.

CodePudding user response:

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;
  • Related