csharp ASP.NET MVC WebViewPageExtensions
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp ASP.NET MVC WebViewPageExtensions相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.WebPages;
using System.Linq;
public static class WebViewPageExtensions
{
/// <summary>
/// Functionality similar to the controller's TryUpdateModel() protected method
/// </summary>
public static bool TryUpdateModel<TModel>(this WebViewPage page, TModel model, string prefix = "") where TModel : class
{
if (model == null)
{
throw new ArgumentNullException("model");
}
var pageModelState = page.ViewData.ModelState;
var bindingContext = new ModelBindingContext
{
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(TModel)),
ModelName = prefix,
ModelState = pageModelState,
//PropertyFilter = propertyFilter,
ValueProvider = page.ViewContext.Controller.ValueProvider
};
var binder = ModelBinders.Binders.GetBinder(typeof(TModel));
binder.BindModel(page.ViewContext.Controller.ControllerContext, bindingContext);
return pageModelState.IsValid;
}
/// <summary>
/// Uses TemplateStack.Stack to return parents
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public static IEnumerable<WebPageRenderingBase> GetPageStack(this WebPageRenderingBase page)
{
var method = typeof(TemplateStack).GetMethod("GetStack", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
var res = method.Invoke(null, new object[] { page.Context }) as Stack<ITemplateFile>;
return res.OfType<WebPageRenderingBase>();
}
/// <summary>
/// Uses TemplateStack.Stack to return parent
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public static WebPageRenderingBase GetPageParent(this WebPageRenderingBase page)
{
return GetPageStack(page).Skip(1).FirstOrDefault();
}
}
以上是关于csharp ASP.NET MVC WebViewPageExtensions的主要内容,如果未能解决你的问题,请参考以下文章
csharp ASP.NET MVC WebViewPageExtensions
csharp ASP.NET的助手,ASP.NET MVC应用服务器端测试
csharp ASP.NET MVC和LINQ
csharp ASP.NET MVC - 表单输入数组
csharp ASP.net MVC:完全禁用缓存
csharp asp.net mvc业务层实现