Skip to content Skip to sidebar Skip to footer

Use Jscript And Controller Actions To Add Data To Database Using Mvc Asp.net

I need to save data to a database from my MVC ASP.net project without refreshing the view page. To do this I plan to create a string and send it to the controller to then send to t

Solution 1:

Try this

javasript

functionCreateChain() {

        //alert("running function createchain");varRecord = document.getElementById("RecordIdTextBox").value;
       varRecordDate = document.getElementById("RecordDateEntry").value;
        ........
var chain= {
            RecordID: Record,
            RecordDate: RecordDate,
           .....
        };
    

        alert( JSON.stringify( chain));
        $.ajax({

            url: "/mycontroller/myaction",
            type: "POST",

            data: { model: chain },

            success: function (result) {
                alert(result);
            },
            error: function (xhr, exception) {
                alert("Error");
            }
        });
    }

Server side

you have to create viewmodel

publicclassChainViewModel
{
 publicint RecordID {get; set;}
  publicstring  RecordDate {get; set;}
   publicstring  Employee {get;set;}
....and so on
}

controller

publicclassMyController :Controller
{

public ActionResult MyAction( ChainViewModel model)
{
    ....use model data to submit to db

    return Ok("Success");
}

}

Post a Comment for "Use Jscript And Controller Actions To Add Data To Database Using Mvc Asp.net"