Tuesday, 15 May 2012

Posting a foreign key using ASP.NET MVC and JQuery autocomplete? -


I want to know the best practice for posting a foreign key.

I have this model.

Public Sector Country {Public Entry ID (Received); Set;} public int details {get; Set;}} Public Class City {Public Entity ID}; Set;} public string description {get; Set;} get public country country; Now in my city, I have to be a dropdown which will not load all the countries in the first load, but I want to do some sort of thing after self-completion, I have seen the method of JQuery to complete it, but I do not know how it is binding in my model.

Can someone give me some advice?

see

You need to provide a view in a text box, such as :

  @ Html.TextBoxFor (m => m.ountry.Id, new {id = "CountryId"})  

before I'm going any further, I agree that your description attribute probably means name , and in the English language, description Meaning of name is a bit misleading.

Use of JQuery

You will need to do two things, first of all, you want to attach completely to your text box:

 < Code> $ ('# Country ID'). Autocomplete ({source: function (request, feedback) {$ .get ('@ GetCountries, "" MyController "), {word: request.term}, function (data) {response ($ .max (data, function (Item) {return {label: item label, value: item.Value}});});}, minlanta: 2})  

response Function is an autocomplete callback that does a few things.

This approach works when you have an AutoComplete, but if your page has more than one autocomplete or onlyIf you want to write script, then add it to autocomplete on each input:

  @ Html.TextBoxFor (M => m.Description, new {data-autocomplete- Url = @ Url.Action ("GetCountries", "MyController"})  

Use of this JQuery:

$ ('* [Data-Automatic : Full-url] ') .ech (function () {$ (this). Autocomplete ({source: function (request, response) {$ .get ($ (this) .data ("autocomplete-url") Finally, the verb method in your controller is:
   

Public JsonResult Ge TCities (string word) {var item = reference.committee. Where (x => x.Description.Contains (term)). Select (x = & gt; nEw {label = x.Id, value = x.Description}). Take (10); Jason comes back (item, Jason.}

where reference is your repository in your case I thought it was a unit framework DbContext < / Code>. . Take () method determines how many values ​​are sent back to the client, keeping this number low can help in response to Jet when auto-complete , But will not return large results sets for short search terms. / P>

As Have been told, you name verbose to your Verbols should try to make it.


No comments:

Post a Comment