MVCToolkit, 我从网上下来的,那个是ms 出的mvc 框架,我怎么编译有错误啊。
错误 1
找不到类型或命名空间名称“ChangeAction”(是否缺少 using 指令或程序集引用?)
E:\ODP.NET samples\文档\MVCToolkit\Blog\Models\ModelPartials.cs还有好几个,大家帮我看看
错误 1
找不到类型或命名空间名称“ChangeAction”(是否缺少 using 指令或程序集引用?)
E:\ODP.NET samples\文档\MVCToolkit\Blog\Models\ModelPartials.cs还有好几个,大家帮我看看
在这里顺便问个问题,就是在第一预览版中,在控制器的Action方法中 可以用如下语句来跳转带ID等参数的URLRedirectToAction(new { Action = "Category", id = product.CategoryID });在第二预览版中反尔不能这样写了,不知道各位是如何来解决这个问题的。
RedirectToAction(new { Controller="Products",Action = "Category", id = product.CategoryID }); 第二预览版写法: System.Web.Routing.RouteValueDictionary rvd=new System.Web.Routing.RouteValueDictionary();
rvd.Add("Controller","Products");
rvd.Add("Action","Category");
rvd.Add("id",product.CategoryID);
RedirectToAction(rvd); 也可以简写成下面一句:
RedirectToAction(new RouteValueDictionary { {"Controller" ,"Products"},{"Action" ,"Category"} ,{"ID",product.CategoryID}});
(1)是: 在页面显示时用到 HTML的扩展功能 例:Html.TextBox(); 在第一预览版中如果不引用MVCToolkit.dll的话 Html 是点不出TextBox等方法的。
但在第二预览版中MVC中已经对这个进行了扩展,所以不需要引用 MVCToolkit.dll(2)是:在控制器接收表单的值传给对象属性时 用到的 UpdateFrom 方法 例如下:
Product product = northwind.GetProductById(id); //Product 是用Linq To Sql 生成的实体类
product.UpdateFrom(Request.Form); (product)
northwind.SubmitChanges(); 这两句代码就可以把表单的值付给product的属性,并提交保存到数据库 在第二版预览版中不知道为什么没有把这个方法 也加到 MVC中 (我想可能是因为此方法容易出错且面向对象的概念不强的原故吧) 不过俺从MVCToolkit中把UpdateFrom复制到我的项目中改了一下编译后同样可以用了。代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Specialized;namespace MyShopAdd2.Controllers
{
public static class BindingHelperExtensions
{
/// <summary>
/// Reads in values from a NameValueCollection (like Request.Form, Cookies, Session, or QueryString) and sets the properties
/// on this object. The names of the values must be Type.PropertyName - i.e. "Product.ProductName"
/// </summary>
/// <param name="values">Request.Form, QueryString, Parameter, etc</param>
public static void UpdateFrom(this object obj, NameValueCollection values)
{
UpdateFrom(obj, values, "");
}
/// <summary>
/// Reads in values from a NameValueCollection (like Request.Form, Cookies, Session, or QueryString) and sets the properties
/// on this object. The names of the values must be Type.PropertyName - i.e. "Product.ProductName"
/// </summary>
/// <param name="values">Request.Form, QueryString, Parameter, etc</param>
/// <param name="objectPrefix">A prefix for the Keys in the Namevalue collection - "Product." in "Product.ProductName" for example</param>
public static void UpdateFrom(this object obj, NameValueCollection values, string objectPrefix)
{
Type objType = obj.GetType();
string objName = objType.Name; PropertyInfo[] fields = objType.GetProperties();
foreach (PropertyInfo property in fields)
{ //check the key
//going to be forgiving here, allowing for full declaration
//or just propname
string httpKey = property.Name; if (objectPrefix != string.Empty)
httpKey = objectPrefix + httpKey; if (values[httpKey] == null)
{
httpKey = objName + "." + property.Name;
} if (values[httpKey] == null)
{
httpKey = objName + "_" + property.Name;
}
if (values[httpKey] != null)
{
TypeConverter conv = TypeDescriptor.GetConverter(property.PropertyType);
object value = values[httpKey]; if (conv.CanConvertFrom(typeof(string)))
{
try
{
value = conv.ConvertFrom(values[httpKey]);
property.SetValue(obj, value, null); }
catch (Exception e)
{
throw new Exception(property.Name + " is not a valid " + property.PropertyType.Name + "; " + e.Message); }
}
else
{
throw new Exception("No type converter available for type: " + property.PropertyType);
}
}
}
} // --------- Class End ---------
}
}