从视图中的javascript向mvc中的函数发送数据
我有一个包含1个函数的类。 如何从myview中的javascript向该函数发送参数? 我怎样才能得到返回值。 我的班级:从视图中的javascript向mvc中的函数发送数据,javascript,asp.net,asp.net-mvc,asp.net-mvc-3,Javascript,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我有一个包含1个函数的类。 如何从myview中的javascript向该函数发送参数? 我怎样才能得到返回值。 我的班级: public class CityClass { public static long GetIdCountryWithCountryText(string countryy) { using (SportContext db = new SportContext()) { return db.
public class CityClass {
public static long GetIdCountryWithCountryText(string countryy)
{
using (SportContext db = new SportContext())
{
return db.tbl_contry.FirstOrDefault(p => p.country== countryy).id;
}
}
}
如何从myview中的javascript向该函数发送参数
你就是不能。javascript对函数一无所知。它不知道什么是C#或静态函数。它也不知道ASP.NETMVC是什么
您可以使用javascript向服务器端点发送AJAX请求,在ASP.NET MVC应用程序的情况下,该端点被称为控制器操作。这个控制器动作可以反过来调用您的静态函数或任何东西
因此,您可以执行以下控制器操作:
public ActionResult SomeAction(string country)
{
// here you could call your static function and pass the country to it
// and possibly return some results to the client.
// For example:
var result = CityClass.GetIdCountryWithCountryText(country);
return Json(result, JsonRequestBehavior.AllowGet);
}
现在,您可以使用jQuery将AJAX请求发送到此控制器操作,并将country javascript变量传递给它:
var country = 'France';
$.ajax({
url: '/somecontroller/someaction',
data: { country: country },
cache: false,
type: 'GET',
success: function(result) {
// here you could handle the results returned from your controller action
}
});
如何从myview中的javascript向该函数发送参数
你就是不能。javascript对函数一无所知。它不知道什么是C#或静态函数。它也不知道ASP.NETMVC是什么
您可以使用javascript向服务器端点发送AJAX请求,在ASP.NET MVC应用程序的情况下,该端点被称为控制器操作。这个控制器动作可以反过来调用您的静态函数或任何东西
因此,您可以执行以下控制器操作:
public ActionResult SomeAction(string country)
{
// here you could call your static function and pass the country to it
// and possibly return some results to the client.
// For example:
var result = CityClass.GetIdCountryWithCountryText(country);
return Json(result, JsonRequestBehavior.AllowGet);
}
现在,您可以使用jQuery将AJAX请求发送到此控制器操作,并将country javascript变量传递给它:
var country = 'France';
$.ajax({
url: '/somecontroller/someaction',
data: { country: country },
cache: false,
type: 'GET',
success: function(result) {
// here you could handle the results returned from your controller action
}
});
好的,谢谢。我可以将结果设置为model吗?我的意思是在我的模型中填充一个字段。例如id。好的,谢谢。我可以将结果设置为model吗?我的意思是在我的模型中填充一个字段。例如id。