Reputation: 47
i have a class that contains 1 function. how can i send a parameter from javascript in myview to this function? and how can i get return value . my class :
public class CityClass {
public static long GetIdCountryWithCountryText(string countryy)
{
using (SportContext db = new SportContext())
{
return db.tbl_contry.FirstOrDefault(p => p.country== countryy).id;
}
}
}
Upvotes: 0
Views: 101
Reputation: 1038720
how can i send a parameter from javascript in myview to this function?
You simply can't. javascript doesn't know anything about functions. It doesn't know what C# or a static function is. It doesn't know what ASP.NET MVC is neither.
You could use javascript to send an AJAX request to a server endpoint which in the case of an ASP.NET MVC application is called controller action. This controller action could in turn invoke your static function or whatever.
So you could have the following controller action:
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);
}
Now you can use jQuery to send an AJAX request to this controller action passing the country javascript variable to it:
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
}
});
Upvotes: 3