Monday, April 25, 2011

Update on ASP.NET MVC Helper for jQuery Ajax ComboBox (Cascading Functionality added)

To use, clear the cascading destination upon selecting from the cascading source. The event of cascading source is on_selected event.

Then on cascading destination use cascaded_word property.

On View:
<div class="editor-label">
    @Html.LabelFor(model => model.PurchasedDto.CategoryId, "Category")
</div>
<div class="editor-field">
    <table>
    <tr>
    <td style="width: 250px">@Html.AjaxComboBoxFor(model => model.PurchasedDto.CategoryId, "/Category/Lookup", "/Category/Caption", 
        new { style="width: 250px" },
        new {
            on_selected = @"$('#PurchasedDto\\.ProductId').ajc().clearValue();" 
            })</td>
    <td>@Html.ValidationMessageFor(model => model.PurchasedDto.CategoryId)</td>
    </tr>
    </table>
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.PurchasedDto.ProductId, "Product")
</div>
<div class="editor-field">
    <table>
    <tr><td style="width: 250px">
    @Html.AjaxComboBoxFor(model => model.PurchasedDto.ProductId, "/Product/Lookup", "/Product/Caption",
    new { style="width: 250px" },
    new
    {
        sub_info = true,
        cascaded_word = "$('input[name=PurchasedDto.CategoryId]').val()"
    })
    </td>           
    <td>@Html.ValidationMessageFor(model => model.PurchasedDto.ProductId)</td>
    </tr>
    </table>        
</div>



On Controller:
[HttpPost]
public JsonResult Lookup(string cascaded_word, string q_word, string primary_key, int per_page, int page_num)
{


    using (var svc = SessionFactoryBuilder.GetSessionFactory().OpenSession())
    {

        int categoryId = 0;

        bool isNumber = int.TryParse(cascaded_word, out categoryId);




        var FilteredProduct = svc.Query<Product>()
                                .Where(x =>
                                    (

                                        categoryId == 0
                                        ||
                                        (categoryId != 0 && x.Category.CategoryId == categoryId)

                                    )

                                    &&

                                    (q_word == "" || x.ProductName.Contains(q_word))

                                    );


        var PagedFilter = FilteredProduct.OrderBy(x => x.ProductName)
                          .LimitAndOffset(per_page, page_num)
                          .Fetch(x => x.Category)
                          .ToList();

        return Json(
            new
            {
                cnt = FilteredProduct.Count()

                ,primary_key = PagedFilter.Select(x => x.ProductId)

                ,candidate = PagedFilter.Select(x => x.ProductName)

                ,cnt_page = PagedFilter.Count()

                ,attached = PagedFilter.Select(x =>
                       new[]
                        {
                            new string[] { "Product Code", x.ProductCode },                                    
                            new string[] { "Category Code", x.Category.CategoryCode },
                            new string[] { "Category", x.Category.CategoryName }
                        }
                   )
            }
            );

    }//using
}

Download the latest to see cascading capability in action: http://code.google.com/p/jquery-ajax-combobox-aspnet-mvc-helper/downloads/list

Get the code from SVN http://code.google.com/p/jquery-ajax-combobox-aspnet-mvc-helper/source/checkout

No comments:

Post a Comment