Tuesday, July 12, 2011

ASP .Net MVC, Linq, AutoComplete

Lots of things today. Lots of bugs. We all know this:

public JsonResult GetHotelsByPartialName(string hotelname) {
            var hotelList = AccommodationService.GetAccommodationByPartialname(hotelname);
            return Json(hotelList);
}

But we need to use a simple viewmodel object call JsonHotel here, cause asp .net mvc is not passing the jsonresult for linq classes without even giving an error probably because of lots of properties :

var accommodations = (from o in adc.Accommodations
                                  where o.HotelName.Contains(hotelname)
                                  select new JsonHotel { HotelID = o.HotelID, HotelName = o.HotelName, Region = o.Region }).ToList();

 In the end the below function is very significant: You can see in that the url being used like url: "/Home/GetHotelsByPartialName?hotelname=" + text instead of json style thats cause that probably wont work but webmethods. Removing all row from the table except of first: $('table').find("tr:gt(0)").remove(). This function was used for a autocomplete textbox to filter on table.
          
        function GetData(text) {
            $.ajax({
                url: "/Home/GetHotelsByPartialName?hotelname=" + text,
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    $('table').find("tr:gt(0)").remove();
                    $.map(data, function (item) {
                        $('table').append(
                                    '<tr><td><a href="/Home/Accommodation/' + item.HotelID + '">Edit</a>' +
                                        '</td><td>' + item.HotelName +
                                        '</td><td>' + item.Region +
                                        '</td></tr>');
                    });
               },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });

No comments:

Post a Comment