User1520731567 posted
Hi zjm_zjm,
how to add jaquery data id to C# function in razor View.
1.If you would like to pass data from controller to view,I suggest you could define ViewBag
in controller and then call it in razor view,
For example:
Controller:
public ActionResult Index()
{
ViewBag.Id
=2;
return View();
}
View:
<div>@ViewBag.Id</div>
OR
2.If you want pass data from view to controller,I suggest you could use
Ajax(),
For example:
View:
$.ajax({
type: "POST",
url: "/Home/AjaxMethod",
data: '{name: "' + $("#txtName").val() + '" }', //you could pass parameters from view to controller
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime);
}
});
Controller:
[HttpPost]
public JsonResult AjaxMethod(string name)
{
...do sth according to name...
PersonModel person = new PersonModel
{
Name = name,
DateTime = DateTime.Now.ToString()
};
...
return Json(person);//then back to the success callback in ajax
}
More details about jQuery AJAX,you could refer to:
https://www.aspsnippets.com/Articles/ASPNet-MVC-Call-Controller-Method-from-View-using-jQuery-AJAX.aspx
Best Regards.
Yuki Tao