Trying to update simpe entity below using UpdateModel. It looks like entity all properties are different in case-insensitive form also.
Calling UpdateModel causes exception
The model of type 'Business.Workflow' could not be updated.
System.InvalidOperationException: The model of type 'Business.Workflow' could not be updated.
at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, IValueProvider valueProvider)
at Erp.Controllers.GridController.AddGeneric[TEntity](TEntity entity)
How to fix or find reason of this exception ?
[Table(Name = "workflow")]
public partial class Workflow : global::Business.EntityBase, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#region entityname
private string _entityname;
[Column(Storage = "_entityname", Name = "entityname", CanBeNull = false, DbType = "character(10)", Expression = null)]
public string Entityname
{
get
{
return _entityname;
}
set
{
if (value != _entityname)
{
_entityname = value;
OnPropertyChanged("Entityname");
}
}
}
#endregion
#region id
private int _id;
[Column(Storage = "_id", Name = "id", DbType = "integer", IsPrimaryKey = true,
IsDbGenerated = true, CanBeNull = false, Expression = "nextval('workflow_id_seq')")]
public int Id
{
get
{
return _id;
}
set
{
if (value != _id)
{
_id = value;
OnPropertyChanged("Id");
}
}
}
#endregion
#region isactive
private bool _isactive;
[Column(Storage = "_isactive", Name = "isactive", DbType = "bool", IsPrimaryKey = false, CanBeNull = false, Expression = null)]
public bool Isactive
{
get
{
return _isactive;
}
set
{
if (value != _isactive)
{
_isactive = value;
OnPropertyChanged("Isactive");
}
}
}
#endregion
#region comment
private string _comment;
[Column(Storage = "_comment", Name = "comment", DbType = "character(50)", IsPrimaryKey = false, CanBeNull = true, Expression = null)]
public string Comment
{
get
{
return _comment;
}
set
{
if (value != _comment)
{
_comment = value;
OnPropertyChanged("Comment");
}
}
}
#endregion
#region script
private string _script;
[Column(Storage = "_script", Name = "script", DbType = "text", IsPrimaryKey = false, CanBeNull = true, Expression = null)]
public string Script
{
get
{
return _script;
}
set
{
if (value != _script)
{
_script = value;
OnPropertyChanged("Script");
}
}
}
#endregion
}
Something else is causing the error. I just tried to create a sample project around the model you showed below and update model worked for me. Can you post the code from your controller action (where you call update model) ?
Thank you. Code is below. UpdateModel for same entity works OK from edit action but fails for Add action below. Debugger shows that HtmldecodeValueprovider is called for all properties. Exception occurs after that.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(string _entity, FormCollection form)
{
var entity = EntityFactory.Create(_entity);
var add = GetType().GetMethod("AddGeneric").MakeGenericMethod(entity.GetType());
add.Invoke(this, new object[] { entity });
}
public void AddGeneric<TEntity>(TEntity entity) where TEntity : EntityBase, new()
{
UpdateModel((TEntity)entity, new HtmlDecodeValueProvider(ControllerContext));
using (var db = new MyDb())
{
entity.InsertOnSubmit<TEntity>(db, null);
db.SubmitChanges();
}
}
I tried your suggestion and also tried to add empty whitelist using code below.
Both forms still cause excpetion. So the issue is not realted to property update. I'm wondering if there are no properties to update, UpdateModel should do nothing, why it throws exception ?
Add controller is same for all entity types. UpdateModel works with other entity types for Add. It also works with all entites if called from update controller (different method). It throws exception only from Add controller and only for this entity type.
How to find reason or fix this ?
UpdateModel<TEntity>(entity, new HtmlDecodeValueProvider(ControllerContext));
UpdateModel<TEntity>(entity, new string[] { }, new HtmlDecodeValueProvider(ControllerContext));
Code is below. Posted data is html encoded so I use this to decode it. Not sure is this best way.
using System;
using System.Globalization;
using System.Web.Mvc;
/// <summary>
/// Decodes html encoded post keys
/// </summary>
public class HtmlDecodeValueProvider : IValueProvider
{
ControllerContext _context;
public HtmlDecodeValueProvider(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
_context = context;
}
bool IValueProvider.ContainsPrefix(string prefix)
{
return _context.HttpContext.Request.Form[prefix] != null;
}
ValueProviderResult IValueProvider.GetValue(string key)
{
var res = _context.HttpContext.Request.Form[key];
if (res == null)
return null;
return new ValueProviderResult(_context.HttpContext.Server.HtmlDecode(res.ToString()),
_context.HttpContext.Server.HtmlDecode(res.ToString()), CultureInfo.CurrentCulture);
}
}
kobruleht
Member
589 Points
463 Posts
How to fix UpdateModel exception: 'myentity' could not be updated
Jul 07, 2011 05:51 PM|LINK
Trying to update simpe entity below using UpdateModel. It looks like entity all properties are different in case-insensitive form also.
Calling UpdateModel causes exception
The model of type 'Business.Workflow' could not be updated.
System.InvalidOperationException: The model of type 'Business.Workflow' could not be updated.
at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, IValueProvider valueProvider)
at Erp.Controllers.GridController.AddGeneric[TEntity](TEntity entity)
How to fix or find reason of this exception ?
[Table(Name = "workflow")] public partial class Workflow : global::Business.EntityBase, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #region entityname private string _entityname; [Column(Storage = "_entityname", Name = "entityname", CanBeNull = false, DbType = "character(10)", Expression = null)] public string Entityname { get { return _entityname; } set { if (value != _entityname) { _entityname = value; OnPropertyChanged("Entityname"); } } } #endregion #region id private int _id; [Column(Storage = "_id", Name = "id", DbType = "integer", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, Expression = "nextval('workflow_id_seq')")] public int Id { get { return _id; } set { if (value != _id) { _id = value; OnPropertyChanged("Id"); } } } #endregion #region isactive private bool _isactive; [Column(Storage = "_isactive", Name = "isactive", DbType = "bool", IsPrimaryKey = false, CanBeNull = false, Expression = null)] public bool Isactive { get { return _isactive; } set { if (value != _isactive) { _isactive = value; OnPropertyChanged("Isactive"); } } } #endregion #region comment private string _comment; [Column(Storage = "_comment", Name = "comment", DbType = "character(50)", IsPrimaryKey = false, CanBeNull = true, Expression = null)] public string Comment { get { return _comment; } set { if (value != _comment) { _comment = value; OnPropertyChanged("Comment"); } } } #endregion #region script private string _script; [Column(Storage = "_script", Name = "script", DbType = "text", IsPrimaryKey = false, CanBeNull = true, Expression = null)] public string Script { get { return _script; } set { if (value != _script) { _script = value; OnPropertyChanged("Script"); } } } #endregion }CodeHobo
All-Star
18647 Points
2647 Posts
Re: How to fix UpdateModel exception: 'myentity' could not be updated
Jul 07, 2011 06:12 PM|LINK
Something else is causing the error. I just tried to create a sample project around the model you showed below and update model worked for me. Can you post the code from your controller action (where you call update model) ?
Blog | Twitter : @Hattan
kobruleht
Member
589 Points
463 Posts
Re: How to fix UpdateModel exception: 'myentity' could not be updated
Jul 07, 2011 06:58 PM|LINK
Thank you. Code is below. UpdateModel for same entity works OK from edit action but fails for Add action below. Debugger shows that HtmldecodeValueprovider is called for all properties. Exception occurs after that.
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Add(string _entity, FormCollection form) { var entity = EntityFactory.Create(_entity); var add = GetType().GetMethod("AddGeneric").MakeGenericMethod(entity.GetType()); add.Invoke(this, new object[] { entity }); } public void AddGeneric<TEntity>(TEntity entity) where TEntity : EntityBase, new() { UpdateModel((TEntity)entity, new HtmlDecodeValueProvider(ControllerContext)); using (var db = new MyDb()) { entity.InsertOnSubmit<TEntity>(db, null); db.SubmitChanges(); } }CodeHobo
All-Star
18647 Points
2647 Posts
Re: How to fix UpdateModel exception: 'myentity' could not be updated
Jul 07, 2011 07:10 PM|LINK
What is the HtmlDecodeValueProvider? What does it's definition look like?
It might be that the problem comes from your usuage of generics and UpdateModel. Have you tried something like this:
Blog | Twitter : @Hattan
kobruleht
Member
589 Points
463 Posts
Re: How to fix UpdateModel exception: 'myentity' could not be updated
Jul 08, 2011 07:11 AM|LINK
I tried your suggestion and also tried to add empty whitelist using code below.
Both forms still cause excpetion. So the issue is not realted to property update. I'm wondering if there are no properties to update, UpdateModel should do nothing, why it throws exception ?
Add controller is same for all entity types. UpdateModel works with other entity types for Add. It also works with all entites if called from update controller (different method). It throws exception only from Add controller and only for this entity type.
How to find reason or fix this ?
UpdateModel<TEntity>(entity, new HtmlDecodeValueProvider(ControllerContext)); UpdateModel<TEntity>(entity, new string[] { }, new HtmlDecodeValueProvider(ControllerContext));Jonathan Che...
Member
525 Points
97 Posts
Re: How to fix UpdateModel exception: 'myentity' could not be updated
Jul 13, 2011 08:37 AM|LINK
Hi Friends,
How do you define HtmlDecodeValueProvider, can you provide some code?
Regards,
Jonathan
kobruleht
Member
589 Points
463 Posts
Re: How to fix UpdateModel exception: 'myentity' could not be updated
Jul 13, 2011 10:18 AM|LINK
Code is below. Posted data is html encoded so I use this to decode it. Not sure is this best way.
using System; using System.Globalization; using System.Web.Mvc; /// <summary> /// Decodes html encoded post keys /// </summary> public class HtmlDecodeValueProvider : IValueProvider { ControllerContext _context; public HtmlDecodeValueProvider(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); _context = context; } bool IValueProvider.ContainsPrefix(string prefix) { return _context.HttpContext.Request.Form[prefix] != null; } ValueProviderResult IValueProvider.GetValue(string key) { var res = _context.HttpContext.Request.Form[key]; if (res == null) return null; return new ValueProviderResult(_context.HttpContext.Server.HtmlDecode(res.ToString()), _context.HttpContext.Server.HtmlDecode(res.ToString()), CultureInfo.CurrentCulture); } }GauriKudtark...
Member
2 Points
1 Post
Re: How to fix UpdateModel exception: 'myentity' could not be updated
Dec 28, 2012 10:03 AM|LINK
Can you tell me which is identity (or primary key)field in your entity?
And are you displaying it in the View?