Look up - Display text

Feb 13, 2011 at 1:13 PM


I've noticed that your current version of the Lookup (single, not multiple) uses the Get(id) function for showing the display text of the selected value.

In our case the Lookup is not using a Repository as data source, but instead a web service call is made, whereas user enter some text for a city name in the search box e.g.

user enters: "Amst"

WebService: input: ("Station text") and output: (List of Station code and Station name pairs)

e.g. Amsterdam #87867324, etc...

The value we use as id is the station code and the station name as a display text.

Now we have an issue in the Get(id) function, because we only have a web method for one direction (station name entered, we get station code), but not the other way around. Another issue would be related to performace since we would make to 2 web service calls unnecessarily because we already get both the name and the code of the station in the first call.

Any suggestion how to resolve this?

Feb 14, 2011 at 3:36 PM

there could be many solutions,

caching: outputcache attribute, wrapping your service calls in a class with your own custom caching, ...

change you web service to have the methods that you need

Feb 14, 2011 at 4:10 PM

We used session for this issue.

Tnx for the suggestions.

Mar 21, 2011 at 9:06 AM

gjanov hi,

what exactly did you do to solve, could you please explain, we have a similar problem



Mar 21, 2011 at 12:27 PM

We have a helper class for creating a json representation of our models:


public class JsonHelper
        public static string Serialize<T>(T obj)
            var serializer = new DataContractJsonSerializer(obj.GetType());
            var ms = new MemoryStream();
            serializer.WriteObject(ms, obj);
            var retVal = Encoding.UTF8.GetString(ms.ToArray());
            return retVal;

        public static T Deserialize<T>(string json)
            var obj = Activator.CreateInstance<T>();
            var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            var serializer = new DataContractJsonSerializer(obj.GetType());
            obj = (T) serializer.ReadObject(ms);
            return obj;


Example of a model is:


    public class NameValuePair
        public string Name { get; set; }

        public string Value { get; set; }


Notice the DataContract and DataMemer attributes... They are relevant for serializing the model in JSON format...

And then in the lookup controler in the search function:


public ActionResult Search(string searchTerm, int? page = null)
            var src = BaseController.GetStations(searchTerm);
            // set a cookie for the results
            var json = JsonHelper.Serialize(src);
            if (Response.Cookies.AllKeys.Contains(Constants.FromStationsCookie))
            Response.Cookies.Add(new HttpCookie(Constants.FromStationsCookie, json));

            if (page.HasValue)
                const int pageSize = 10;
                var rows = this.RenderView(@"Awesome\LookupList", src.Skip((page.Value - 1) * pageSize).Take(pageSize));


                return Json(new { rows, more = src.Count() > page * pageSize });
            return View(@"Awesome\LookupList", src);


and the Get function looks like this:


public ActionResult Get(string id)
            var json = Request.Cookies[Constants.FromStationsCookie];
            if (json != null)
                var jsonValue = json.Value;
                if (!String.IsNullOrEmpty(jsonValue))
                    var list = JsonHelper.Deserialize<List<NameValuePair>>(jsonValue);

                    return Content(list.FirstOrDefault(item => item.Value == id).Name);

            return Content("");


Also u can use session for this... I hope it helps...


Mar 24, 2011 at 6:38 AM