开发者

Updating Views ASP NET MVC 3.0

How to refresh the view using the controller only.Is there something like ?

public ActionResult Index()
{

     [Controller开发者_开发百科(Update = 10)]

}


Create an "AutoRefresh" action attribute that injects a meta refresh tag:

public class AutoRefreshAttribute : ActionFilterAttribute
{
    public const int DefaultDurationInSeconds = 300; // 5 Minutes

    public AutoRefreshAttribute()
    {
        DurationInSeconds = DefaultDurationInSeconds;
    }

    public int DurationInSeconds
    {
        get;
        set;
    }

    public string RouteName
    {
        get;
        set;
    }

    public string ControllerName
    {
        get;
        set;
    }

    public string ActionName
    {
        get;
        set;
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        string url = BuildUrl(filterContext);
        string headerValue = string.Concat(DurationInSeconds, ";Url=", url);

        filterContext.HttpContext.Response.AppendHeader("Refresh", headerValue);

        base.OnResultExecuted(filterContext);
    }

    private string BuildUrl(ControllerContext filterContext)
    {
        UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext);
        string url;

        if (!string.IsNullOrEmpty(RouteName))
        {
            url = urlHelper.RouteUrl(RouteName);
        }
        else if (!string.IsNullOrEmpty(ControllerName) && !string.IsNullOrEmpty(ActionName))
        {
            url = urlHelper.Action(ActionName, ControllerName);
        }
        else if (!string.IsNullOrEmpty(ActionName))
        {
            url = urlHelper.Action(ActionName);
        }
        else
        {
            url = filterContext.HttpContext.Request.RawUrl;
        }

        return url;
    }
}

Then use it like this:

[AutoRefresh(DurationInSeconds = 10)]
public ActionResult Index()
{

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜