0

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;
        }
    }
}

1 Answer 1

3

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    
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

ok thanks . can i set the result to model? i mean fill a field in my model . for example id .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.