ASP.NET MVC将HTML表内容传输到另一个视图

本文关键字:传输 另一个 视图 NET MVC HTML ASP | 更新日期: 2023-09-27 18:06:33

这可行吗?我的情况是这样的,如果我正在尝试做的事情不可取,你能建议一个更简单或更有效的方法吗?我们这里讨论的是报告生成页面。首先,我有一个存储过程,如果没有设置过滤器/条件,它需要很长时间才能完成执行。这意味着它是一个视图,这个存储过程返回一个列表。然后,该列表在我的视图中填充一个表。它可能只有10到数千条记录,但执行时间很长,因为它要根据数千条记录计算这个和那个,为了简短起见,我不会改变我的存储过程。

现在,从第一个视图中,我有一个可打印版本按钮,它调用具有相同内容但打印友好的另一个页面。我不想执行痛苦的存储过程来获得相同的列表,我想重用已经生成的列表。我该怎么做呢?

ASP.NET MVC将HTML表内容传输到另一个视图

据我所知,您正在考虑实现某种方法来缓存您通过缓慢的存储过程计算的数据?

一种选择是实现一个CacheManager并将结果缓存一段时间:

/// <summary>
/// Cache Manager Singleton
/// </summary>
public class CacheManager
{
    /// <summary>
    /// The instance
    /// </summary>
    private static MemoryCache instance = null;
    /// <summary>
    /// Gets the instance of memoryCache.
    /// </summary>
    /// <value>The instance of memoryCache.</value>
    public static MemoryCache Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new MemoryCache();
            }
            return instance;
        }
    }
}
/// <summary>
/// Cache Manager
/// </summary>
public class MemoryCache
{
    /// <summary>
    /// Gets the expiration date of the object
    /// </summary>
    /// <value>The no absolute expiration.</value>
    public DateTime NoAbsoluteExpiration
    {
        get { return DateTime.MaxValue; }
    }
    /// <summary>
    /// Retrieve the object in cache
    /// If the object doesn't exist in cache or is obsolete, getItemCallback method is called to fill the object
    /// </summary>
    /// <typeparam name="T">Object Type to put or get in cache</typeparam>
    /// <param name="httpContext">Http Context</param>
    /// <param name="cacheId">Object identifier in cache - Must be unique</param>
    /// <param name="getItemCallback">Callback method to fill the object</param>
    /// <param name="slidingExpiration">Expiration date</param>
    /// <returns>Object put in cache</returns>
    public T Get<T>(string cacheId, Func<T> getItemCallback, TimeSpan? slidingExpiration = null) where T : class
    {
        T item = HttpRuntime.Cache.Get(cacheId) as T;
        if (item == null)
        {
            item = getItemCallback();
            if (slidingExpiration == null)
            {
                slidingExpiration = TimeSpan.FromSeconds(30);
            }
            HttpRuntime.Cache.Insert(cacheId, item, null, this.NoAbsoluteExpiration, slidingExpiration.Value);
        }
        return item;
    }
    /// <summary>
    /// Retrieve the object in cache
    /// If the object doesn't exist in cache or is obsolete, null is returned
    /// </summary>
    /// <typeparam name="T">Object Type to put or get in cache</typeparam>
    /// <param name="httpContext">Http Context</param>
    /// <param name="cacheId">Object identifier in cache - Must be unique</param>
    /// <returns>Object put in cache</returns>
    public T Get<T>(string cacheId) where T : class
    {
        T item = HttpRuntime.Cache.Get(cacheId) as T;
        if (item == null)
        {
            return null;
        }
        return item;
    }
    /// <summary>
    /// Delete an object using his unique id
    /// </summary>
    /// <param name="httpContext">Http Context</param>
    /// <param name="cacheId">Object identifier in cache</param>
    /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
    public bool Clear(string cacheId)
    {
        var item = HttpRuntime.Cache.Get(cacheId);
        if (item != null)
        {
            HttpRuntime.Cache.Remove(cacheId);
            return true;
        }
        return false;
    }
    /// <summary>
    /// Delete all object in cache
    /// </summary>
    /// <param name="httpContext">Http Context</param>
    public void ClearAll(string filter = null)
    {
        var item = HttpRuntime.Cache.GetEnumerator();
        while (item.MoveNext())
        {
            DictionaryEntry entry = (DictionaryEntry)item.Current;
            var key = entry.Key.ToString();
            if (filter != null && (key.ToLower().Contains(filter.ToLower()) || filter == "*" )) //if filter, only delete if the key contains the filter value
            {
                HttpRuntime.Cache.Remove(entry.Key.ToString());
            }
            else if (filter == null) //no filter, delete everything
            {
                HttpRuntime.Cache.Remove(entry.Key.ToString());
            }
        }
    }
}

注:这不是我自己写的,但找不到原始来源。

用法如下:

// Retrieve object from cache if it exist, callback is performed to set and return object otherwise
UserObject userObject = CacheManager.Instance.Get(httpContext, "singleId", ()=> {
    return new UserObject() {
        Id = Guid.NewGuid()
    };
});
// Retrieve object from cache if it exist, return  null otherwise
UserObject userObjectRetrieved = CacheManager.Instance.Retrieve<UserObject>(httpContext, "singleId");
// Remove object from cache
CacheManager.Instance.Clear(httpContext, "singleId");
// Remove all object from cache
CacheManager.Instance.ClearAll(httpContext);

不知道你的视图设计是什么意思,但你可以同时填充可打印的版本,并隐藏它,直到按钮被点击