Показ дописів із міткою mvc. Показати всі дописи
Показ дописів із міткою mvc. Показати всі дописи

неділя, 9 жовтня 2011 р.

MVC: Cascading Dropdown

Action Methods:
[HttpPost]
public JsonResult GetCategories()
{
    Thread.Sleep(1000);

    return Json(categoriesList);
}

[HttpPost]
public JsonResult GetProducts(int? category)
{
    Thread.Sleep(1000);

    var list = category.HasValue ? productsList.Where(x => x.CategoryId == category).ToList() : productsList;

    return Json(list);
}
HTML:
<lable for="category">Category</lable>
<select id="category" name="category">
<option value="-1">Select...</option>
@foreach (var item in Model)
{
    <option value="@item.Id">@item.Title</option>
}
</select>
<lable for="product">Product</lable>
<select id="product" name="product">
    <option value="-1">Select cetegory...</option>
</select>
Script:
$(function () {
        var categories = $('#category');
        var products = $('#product');

        products.change(function () {
            console.log(products.val());
        });

        categories.change(function () {
            var id = categories.val();

            if (id != -1) {
                products.empty();
                products.append($('<option/>', {
                    value: -1,
                    text: "Loading..."
                }));

                $.ajax({
                    type: 'POST',
                    url: "/Products/GetProducts",
                    data: { category: id },
                    success: function (resp) {
                        var items = eval(resp);

                        products.empty();
                        products.append($('<option/>', {
                            value: -1,
                            text: "Select product..."
                        }));

                        $.each(items, function (index, item) {
                            products.append($('<option/>', {
                                value: item.Id,
                                text: item.Title
                            }))
                        });
                    },
                    error: function () {
                        console.log('Error');
                        console.log(arguments);
                    }
                });
            } else {
                products.empty();
                products.append($('<option/>', {
                    value: -1,
                    text: "Select cetegory..."
                }));
            }
        });
    });

четвер, 4 серпня 2011 р.

MVC 3: disable browser cache


public class NoCache : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();

base.OnResultExecuting(filterContext);
}
}

четвер, 9 червня 2011 р.

MVC 3 Content with no cache

Simple helper:

public static class UrlHelperExtensions
{
public static string ContentNoCache(this UrlHelper url, string path)
{
return url.Content("{0}?_dc={1}".FormatWith(path, DateTime.Now.ToFileTime()));
}
}

where FormatWith:

public static string FormatWith(this string input, params object[] args)
{
return string.Format(input, args);
}

Use:
<script src="@Url.ContentNoCache("~/Scripts/app.js")" type="text/javascript"></script>
Result:
<script src="/Scripts/app.js?_dc=129520920501677826" type="text/javascript"></script>