0

This is Ajax code

<script>

    $(document).ready(function () {

        $("#btnKaydet").click(function () {

            debugger
           
            var data = $("#myForm").serialize();

            $.ajax({

                type:"POST",
                url: "/Home/Olusturma",
                data: data,
                success: function (response) {

                    alert("Kayıt İşlemi Başarılı")
                }
            })
        })
    })

</script>

This is saving button code

<form id="myForm">
    <input type="button" value="Buradan Kaydet" class=" btn btn-default" id="btnKaydet" />
</form>

This is HomeController code

public ActionResult Olusturma()
    {
        KullaniciDBEntities db = new KullaniciDBEntities();

        List<Kisi> kL = db.Kisi.ToList();
        ViewBag.kisiList = new SelectList(kL, "AdSoyad", "Email", "Yas", "Konum", "Telefon", "Parola");

        return View();
    }

    [HttpPost]
    public ActionResult Olusturma(KullaniciDBEntities model)
    {
        try
        {
            KullaniciDBEntities db = new KullaniciDBEntities();
            List<Kisi> kL = db.Kisi.ToList();
            ViewBag.kisiList = new SelectList(kL, "AdSoyad", "Email", "Yas", "Konum", "Telefon", "Parola");

            Kisi k = new Kisi();

            k.ID = model.ID;
            k.AdSoyad = model.AdSoyad;
            k.Email = model.Email;
            k.Yas = model.Yas;
            k.Konum = model.Konum;
            k.Telefon = model.Telefon;
            k.Parola = model.Parola;
                

            db.Kisi.Add(k);
            db.SaveChanges();

            int SonId = k.ID;
        }
        catch (Exception ex)
        {

            throw ex;
        }
        return RedirectToAction("Listeleme");

    }

Hello everyone. When ı run my project, ı get a error. My variables seems normal but they dont show anything.It shows (null) variable in their places .How can ı fix this error? I have controller, KullaniciDBentities model and View parts.

5
  • Why dont you use your Model? Maybe use document.getElementsByName? Commented Oct 8, 2020 at 8:50
  • Actually I can add and use my own model. But it brings the null values . Commented Oct 8, 2020 at 11:44
  • I notice you are redirecting at the end of your Post. When you redirect you can pass on the model. Example return RedirectToAction("Listeleme", k); I am not sure if Kisi k is your model, but look at the method RedirectToAction. Remember you loose the model because your are redirecting to another action. Commented Oct 8, 2020 at 12:56
  • Take a look at this post, explaining returning model. stackoverflow.com/questions/4013695/… Commented Oct 8, 2020 at 12:58
  • Still not perfect but improved formatting. Commented Oct 9, 2020 at 17:08

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.