首页 > 代码库 > MVC 学习随笔(一)
MVC 学习随笔(一)
Model的绑定。
(一)使用NameValueCollectionValueProvider
C# 对NameValueCollectionValueProvider的支持是通过下面的类实现的
// Library部分 public class NameValueCollectionValueProvider : IUnvalidatedValueProvider, IValueProvider{// 构造函数 public NameValueCollectionValueProvider(NameValueCollection collection, CultureInfo culture);// 实现IValueProvider public virtual ValueProviderResult GetValue(string key);// 省略} [Serializable] public class ValueProviderResult { public object RawValue { get; protected set; } public object ConvertTo(Type type); }
下面介绍一个简单的应用
// 应用部分//Controller中定义NameValueCollectionValueProvider 作为显示Model传递给View NameValueCollection collection = new NameValueCollection(); collection.Add("bar.Name", "John"); collection.Add("bar.Age", "12"); collection.Add("bar.Address.Street", "HongKang Street"); collection.Add("bar.Address.ZipCode", "021"); NameValueCollectionValueProvider nProvider = new NameValueCollectionValueProvider(collection, CultureInfo.CurrentCulture); var test = nProvider.GetValue("bar.Name"); return View(nProvider);
在View中接收来自Controller的传递
@Model NameValueCollectionValueProvider <table> <tr> <th colspan="2">Bar</th> </tr> <tr> <td>bar.Name</td> <td>@Model.GetValue("bar.Name").ConvertTo(typeof(string))</td> </tr> <tr> <td>bar.Age</td> <td>@Model.GetValue("bar.Age").ConvertTo(typeof(string))</td> </tr> <tr> <th colspan="2">Bar.Address</th> </tr> <tr> <td>bar.Address.Street</td> <td>@Model.GetValue("bar.Address.Street").ConvertTo(typeof(string))</td> </tr> <tr> <td>bar.Address.ZipCode</td> <td>@Model.GetValue("bar.Address.ZipCode").ConvertTo(typeof(string))</td> </tr> </table>
继承自NameValueCollectionValueProvider的类有下面几个:
1.1: FormValueProvider 传递Form中控件的值
@using (Html.BeginForm("TestFormValueProvider", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="text" name="testBox" value=http://www.mamicode.com/"" /> <input type="submit" value=http://www.mamicode.com/"提交" title="提交" /> }
在Controller中将获取Form中提交的控件值
public ActionResult TestFormValueProvider() { FormValueProvider form = new FormValueProvider(ControllerContext); var testBoxValue = http://www.mamicode.com/form.GetValue("testBox"); var outPut = testBoxValue.ConvertTo(typeof(string)); Console.WriteLine(outPut); return null; }
1.2: QueryStringValueProvider传递查询字符串
// HTML 代码片段 <a href="@Url.Action("TestQueryStringProvider", "Home", new { Name="John Shen", Age="32"})" title="actionTest">测试URL QueryString传递Model</a>
Controller中获取View中的<a></a>的查询字符串
public ActionResult TestQueryStringProvider() { QueryStringValueProvider query = new QueryStringValueProvider(ControllerContext); var testNameQueryString = query.GetValue("Name").ConvertTo(typeof(string)); var testAgeQueryString = query.GetValue("Age").ConvertTo(typeof(string)); var testCompanyQueryString = query.GetValue("Company").ConvertTo(typeof(string)); Console.WriteLine(testNameQueryString.ToString() + testAgeQueryString.ToString() + testCompanyQueryString.ToString()); return null; }
(二)使用DictionaryValueProvider传递Model
NameValueCollectionValueProvider的是一个不对Key进行唯一性约束的键值列表,并且其Value只能是字符串。而DictionaryValueProvider才是真正意义上的键值对。Key有唯一性约束、Value可以是任何之。继承自DictionaryValueProvider的子类有以下几种
2.1: HttpFileCollectionValueProvider上传文件的绑定
HTML:
@using (Html.BeginForm("TestFormValueProvider", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="text" name="testBox" value="" /> <input type="submit" value="提交" title="提交" /> <input type="file" name="file1"/> <input type="file" name="file2"/> <input type="file" name="file3"/> <input type="file" name="file1"/> }
Controller中获取从Html中上传的文件
HttpFileCollectionValueProvider files = new HttpFileCollectionValueProvider(ControllerContext); object test = files.GetValue("file1").ConvertTo(typeof(HttpPostedFileWrapper[])); var tes = test as HttpPostedFileWrapper[]; foreach (var t in tes) { t.SaveAs(Server.MapPath("../Content") + "/"+t.FileName); }
因为文件上传控件中有两个Name是file1的。所以得到的是两个文件
2.2: RouteDataValueProvider传递RouteData中的Values属性到View
public ActionResult Index() { // Pass data in RouteDataDictionary RouteDataValueProvider provider = new RouteDataValueProvider(ControllerContext); return View(provider); }
View中接收来自Controller的 Model
@Model RouteDataValueProvider<h2>Index</h2><h3>@Model.GetValue("controller").ConvertTo(typeof(string))</h3><h3>@Model.GetValue("action").ConvertTo(typeof(string))</h3>
2.3: ChildActionValueProvider传递RouteData中的Value到子Action中的View
MVC中有很多HTMLHelper的扩展方法用于生成当前View的子View
// Summary: // Represents support for calling child action methods and rendering the result // inline in a parent view. public static class ChildActionExtensions { // Summary: // Invokes the specified child action method and returns the result as an HTML // string. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the action method to invoke. // // Returns: // The child action result as an HTML string. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName); // // Summary: // Invokes the specified child action method with the specified parameters and // returns the result as an HTML string. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the action method to invoke. // // routeValues: // An object that contains the parameters for a route. You can use routeValues // to provide the parameters that are bound to the action method parameters. // The routeValues parameter is merged with the original route values and overrides // them. // // Returns: // The child action result as an HTML string. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, object routeValues); // // Summary: // Invokes the specified child action method using the specified parameters // and returns the result as an HTML string. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the action method to invoke. // // routeValues: // A dictionary that contains the parameters for a route. You can use routeValues // to provide the parameters that are bound to the action method parameters. // The routeValues parameter is merged with the original route values and overrides // them. // // Returns: // The child action result as an HTML string. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, RouteValueDictionary routeValues); // // Summary: // Invokes the specified child action method using the specified controller // name and returns the result as an HTML string. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the action method to invoke. // // controllerName: // The name of the controller that contains the action method. // // Returns: // The child action result as an HTML string. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName); // // Summary: // Invokes the specified child action method using the specified parameters // and controller name and returns the result as an HTML string. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the action method to invoke. // // controllerName: // The name of the controller that contains the action method. // // routeValues: // An object that contains the parameters for a route. You can use routeValues // to provide the parameters that are bound to the action method parameters. // The routeValues parameter is merged with the original route values and overrides // them. // // Returns: // The child action result as an HTML string. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues); // // Summary: // Invokes the specified child action method using the specified parameters // and controller name and returns the result as an HTML string. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the action method to invoke. // // controllerName: // The name of the controller that contains the action method. // // routeValues: // A dictionary that contains the parameters for a route. You can use routeValues // to provide the parameters that are bound to the action method parameters. // The routeValues parameter is merged with the original route values and overrides // them. // // Returns: // The child action result as an HTML string. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues); // // Summary: // Invokes the specified child action method and renders the result inline in // the parent view. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the child action method to invoke. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static void RenderAction(this HtmlHelper htmlHelper, string actionName); // // Summary: // Invokes the specified child action method using the specified parameters // and renders the result inline in the parent view. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the child action method to invoke. // // routeValues: // An object that contains the parameters for a route. You can use routeValues // to provide the parameters that are bound to the action method parameters. // The routeValues parameter is merged with the original route values and overrides // them. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static void RenderAction(this HtmlHelper htmlHelper, string actionName, object routeValues); // // Summary: // Invokes the specified child action method using the specified parameters // and renders the result inline in the parent view. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the child action method to invoke. // // routeValues: // A dictionary that contains the parameters for a route. You can use routeValues // to provide the parameters that are bound to the action method parameters. // The routeValues parameter is merged with the original route values and overrides // them. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static void RenderAction(this HtmlHelper htmlHelper, string actionName, RouteValueDictionary routeValues); // // Summary: // Invokes the specified child action method using the specified controller // name and renders the result inline in the parent view. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the child action method to invoke. // // controllerName: // The name of the controller that contains the action method. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static void RenderAction(this HtmlHelper htmlHelper, string actionName, string controllerName); // // Summary: // Invokes the specified child action method using the specified parameters // and controller name and renders the result inline in the parent view. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the child action method to invoke. // // controllerName: // The name of the controller that contains the action method. // // routeValues: // An object that contains the parameters for a route. You can use routeValues // to provide the parameters that are bound to the action method parameters. // The routeValues parameter is merged with the original route values and overrides // them. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static void RenderAction(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues); // // Summary: // Invokes the specified child action method using the specified parameters // and controller name and renders the result inline in the parent view. // // Parameters: // htmlHelper: // The HTML helper instance that this method extends. // // actionName: // The name of the child action method to invoke. // // controllerName: // The name of the controller that contains the action method. // // routeValues: // A dictionary that contains the parameters for a route. You can use routeValues // to provide the parameters that are bound to the action method parameters. // The routeValues parameter is merged with the original route values and overrides // them. // // Exceptions: // System.ArgumentNullException: // The htmlHelper parameter is null. // // System.ArgumentException: // The actionName parameter is null or empty. // // System.InvalidOperationException: // The required virtual path data cannot be found. public static void RenderAction(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues); }
首先定义一个View在它里面包含一个子的View(通过以上的扩展方法)
<div> @Html.Action("ChildActionMethod","Test", new {Name="John", Age="32"});</div>
接着在他所指向的Controller中我们接收这个扩展方法的ChildActionValueProvider
public ActionResult ChildActionMethod() { ChildActionValueProvider child = new ChildActionValueProvider(ControllerContext); return View(child); }
改Action方法指向的View就是子View中的内容
<h3>@Model.GetValue("Name").ConvertTo(typeof(string))</h3><h3>@Model.GetValue("Age").ConvertTo(typeof(string))</h3>
呈现的效果类似于:
MVC 学习随笔(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。