I have created a button and added a jquery click event to that as below.
$("#saveMe").click(function ()
{
var car = { name: 'intro to asp.net', price:5200};
$.ajax({
type: "POST",
url: "/Book/saveData",
data: car,
datatype: "html",
success: function (data) {
alert("successfully saved in DB");
}
});
});
I need to access these JSON data in controller action. I need to to get the value for name and price separately.And also i need to assign the name value for name attribute in book model and price value for price attribute in book mode. My controller action is like below.
public ActionResult saveData()
{
String name = //assign json data.name.
double price= //assign json.data.price
book.name=name // **Is this a correct way to assign values to the mode?**
}
I am a beginner in asp.net and I really can't solve this problem.Please help me. Thank you.
Add the parameters to your method so its
public ActionResult saveData(string name, decimal price)
{
...
}
or create a model (say class Book
) with 2 properties ( string name
and decimal price
) so the model is bound with the values
public ActionResult saveData(Book model)
It should be:
[HttpPost]
public JsonResult saveData(string name, double price) { ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.