我正在尝试使用UpdateModel在我的控制器中从FormCollection赋值。控制器看起来像:
public ActionResult EditValues(int id, FormCollection collection)
{
NamedClass picture = PictureProvider.GetById(id);
try
{
if(ModelState.IsValid)
{
UpdateModel(picture, collection);
}
}
catch {}
}在string UpdateModel(picture, collection);中,我得到了一个ArgumentException,上面写着“已经添加了具有相同键的元素”,仅此而已。Collection和图片有相同的字段..我不想在“集合”中添加任何可能导致这种异常的东西..尝试手动为图片的字段赋值-确定。有没有人见过这样的把戏?提前谢谢你!更新:也许StackTrace会有帮助..
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
System.Web.Mvc.ModelBindingContext.get_PropertyMetadata()
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext)
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model)
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, IValueProvider valueProvider)Namedclass是:
public class NamedClass
{
[Key]
public virtual int id {get; set;}
public virtual string username {get; set;}
}提供值的视图是:
@model project.Models.NamedClass
@using (Html.BeginForm("EditValues", "PicController", FormMethod.Post))
{
@Html.HiddenFor(m=>m.id)
<div class="display-label">@Html.LabelFor(model=>model.username)</div>
<div class="display-field">
@Html.EditorFor(model => model.username)
</div>
<input type="submit" value="save" />
}发布于 2012-07-18 12:58:03
为什么你不能这样做呢。
public ActionResult EditValues(NamedClass model)
{
if(ModelState.IsValid)
{
.. save to db
}
return View();
}https://stackoverflow.com/questions/11527833
复制相似问题