Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the database table in button click?

I am working on the MVC3 with ADO.Net Connectivity, an then i am updating the div with jQuery

Here i my code

Controller

public ActionResult GetData()
{
 return this.Json(_db.MYMOVIELISTs, JsonRequestBehavior.AllowGet);
}
public void Insert(string Id)
{
    var movieToInsert = new MYMOVIELIST { Title = "Ben", Director = "LEMe", ID = Id };
    // Save new movie to DB
    _db.AddToMYMOVIELISTs(movieToInsert);
    _db.SaveChanges();
}

ClientSide

function insertCallback(result) {
    readData();
}
function readData() {
    info.empty();
    $.ajax({
        type: "GET",
        url: 'Home/GetData',
        async: false,
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                var panel = $("<div class='panel' ></div>")
                var d = data[i];
                panel.append("ID: " + d.ID + "</br>");
                panel.append("Title: " + d.Title + "</br>");
                panel.append("Director: " + d.Director + "</br>");
                panel.append("<hr>");
                panel.data("info", d);
                panel.appendTo(info);
            }
        }
    });
}
$("#btnAdd").click(function () {
    $.post("Home/Insert", $("form").serialize(), insertCallback);
});

This is works Fine, my problem is i want to update the Database table in "Save" buttom click. i tried to call the _db.SaveChanges(); in save button click instead of the Insert it is not adding the movieToInsert to table, here i want to know is how to save the database later, Here any thing am doing wrong or is there any best approach for DB connectivity

like image 511
BalaKrishnan웃 Avatar asked Nov 19 '25 22:11

BalaKrishnan웃


1 Answers

You are serializing a form, so eventually you may want below... which will bind your serialized form to the a movie model/entity:

[AcceptVerbs("POST")]
public void Insert(MovieEntity movie)

As for database practice, I recommend reading about the repository pattern and dependency injection as it pertains to ASP.NET MVC.

like image 178
Jonathan Harrison Avatar answered Nov 22 '25 12:11

Jonathan Harrison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!