使用UpdateModel的正确方法是什么?
当我运行这个:
TryUpdateModel返回true,ViewData没有错误,Proxy没有更新。动作法
public void Save(string TypeName, int Id, FormCollection idontknow) {
var types = Assembly.GetExecutingAssembly().GetTypes();
var ObjectType=(from t in types where t.Name == TypeName select t).First();
var Proxy = context.Set(ObjectType).Find(Id); // EF 4.1
if (TryUpdateModel(Proxy, TypeName)) {
var x = ViewData.GetModelStateErrors(); // no errors
}
}发布的数据
TypeName=Thing&Id=1&Thing.Id=1&Thing.Name=hello&Thing.OptionID=2事物类
public class Thing : Base {
public virtual Nullable<int> OptionID { get; set; }
public virtual Option Option { get; set; }
public virtual ICollection<ListItem> ListItems { get; set; }
}
public class Base {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
[NotMapped]
public virtual int? EntityState { get; set; }
}编辑:我也尝试显式地传递表单集合TryUpdateModel(Proxy, TypeName, idontknow)
编辑#2: (响应NickLarsen)
发布于 2011-07-29 03:01:51
我删除了所有的EF内容,并试图只获取查询字符串来填充模型中的值.而且效果很好。
//controller class
public ActionResult Save(string TypeName, int Id, FormCollection idontknow)
{
var Proxy = new Thing
{
Id = 33,
OptionID = 2234,
Name = "tony",
};
if (TryUpdateModel(Proxy, TypeName))
{
ViewBag.Message = "WInner";
}
return RedirectToAction("Index");
}
//end controller class
public class Thing : Base
{
public virtual Nullable<int> OptionID { get; set; }
}
public class Base
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}老实说,我想不出您的代码中有什么可以阻止它工作,但我建议逐个查看列表,并在每一步之后进行测试。
https://stackoverflow.com/questions/6867909
复制相似问题