c# - Image Field required when updating/Editing a table with EF and MVC -


i have abit of confusing issue here. creating blog in mvc. users of blog can post blog. have changed post table in enitity framework include ability upload picture (varbinary(max)).

i have handled in user create post controller action , can upload , display pictures on users posts.

however problem comes when administrator has allow publishing of post (simple controller scaffolding edit). (no user can post without approval) have field in post table called published (bit, not null).

when try edit post of user publish admincontroller, error when save changes:

myblogger.models.post: picture field required.

    [httppost]     [validateantiforgerytoken]     public actionresult edit(post post)     {         try         {             if (modelstate.isvalid)             {                 db.entry(post).state = entitystate.modified;                 db.savechanges();                 return redirecttoaction("index");             }         } 

the problem difficult work out because in edit view cant @html.picture(m => m.picture) in order display picture, have convert byte base64 , display have this:

src="@html.raw("data:image/jpeg;base64," + viewbag.imagetoshow)"  

i dont have option either in edit action db.addorupdate(post); have db.savechanges();

all im doing edit action

find post specified id, update publish field of specific post, save changes db.

im not sure why grumbling saying picture field required? either because table entry picture field set not null or because saving table entries post again, presents problem of adding/updating?

when post modified model post model binder cannot bind image picture property again. if want modify of entity's properties, can fetch , modify desired properties , save again.

[httppost] public actionresult edit(post post) {     try     {         if (modelstate.isvalid)         {             post oldpost = db.posts.find(post.id);             oldpost.published=post.published;             // repeat others.              db.savechanges();             return redirecttoaction("index");         }     }     catch (system.data.entity.validation.dbentityvalidationexception dbex)     {        //     } } 

but if want modify picture property must use httppostedfilebase class new image , save above.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -