Kadir GÜNEY Kadir GÜNEY - 3 months ago 13
Javascript Question

ASP Net Mvc js promp value save database

I want to save the entered value of the database. However, values are sent as null.
I'm glad if I encode additional help.

HomeConteller.cs

[HttpPost]
public void GaleriOlustur(string Adi)
{
GaleriTanim As = new GaleriTanim() { Adi = Adi };
db.GaleriTanims.Add(As);
db.SaveChanges();
islemler islem = new islemler { islemler1 = "Galeri Oluşturuldu", kayitTarihi = DateTime.Now };
db.islemlers.Add(islem);
db.SaveChanges();
RedirectToAction("GaleriYonet", "Home");

}


GaleriYonet.cshtml

@using (Html.BeginForm("GaleriOlustur", "Home",FormMethod.Post,new {Adi="Adi"}))
{
<input type="submit" onclick="GaleriOlustur()" name="Adi" value="Galeri Oluştur"/>
}


GaleriYonet.cshtml "Javascript"

<script type="text/javascript">

function GaleriOlustur() {
var Adi = prompt("Galeri İsmi Giriniz");
if (Adi != null) {
return Adi;
} else {
alert("Bir İsim Girmelisiniz.");
return false;
}
};



Answer

Your client side function should either return true or false. Not the value user entered to the prompt.

I also suggest you keep an input variable value (hidden type) in your form with name matching to your action method parameter name. In your javascript method, when user enter a vliad value, you can update this form control value to that.

Also, you need to do return GaleriOlustur() on the onclick event

@using (Html.BeginForm("GaleriOlustur", "Home", FormMethod.Post, new { Adi = "Adi" }))
{
    <input type="hidden" name="Adi" />
    <input type="submit" onclick="return GaleriOlustur()" value="Galeri Oluştur" />
}

and in js method, set the input field value to the value user entered.

function GaleriOlustur() {
        var adi = prompt("Galeri İsmi Giriniz");
        if (adi !=="") {
            $("input[name='Adi']").val(adi);
            return true;
        } else {
            alert("Bir İsim Girmelisiniz.");
            return false;
        }
};