keep values of model's unused members during updateshttp://forums.asp.net/t/1543075.aspx/1?keep+values+of+model+s+unused+members+during+updatesFri, 02 Apr 2010 17:41:38 -040015430753764243http://forums.asp.net/p/1543075/3764243.aspx/1?keep+values+of+model+s+unused+members+during+updateskeep values of model's unused members during updates <p>hi,</p> <p>suppose our model has a property named &quot;CreatedByUserId&quot; that keeps the creator's user id</p> <p>when we want to update our model, there are no need to display this field, but we should keep it's value during the update.</p> <p>so, if i don't place any edit field for this property on the view, the model wouldn't have any value for CreatedByUserId property when returns to controller</p> <p>to solve this, i :</p> <p>1.place a hidden input in the view for these fields (which is vulnerable)&nbsp;</p> <p>or</p> <p>2.make a Get call to db and get the original CreatedByUserId value on each update (which causes additional&nbsp;round trips&nbsp;to db)</p> <p><br> </p> <p>isn't there any better way to do this ?</p> <p><br> </p> <p>thanks in advance</p> 2010-04-02T11:32:14-04:003764313http://forums.asp.net/p/1543075/3764313.aspx/1?Re+keep+values+of+model+s+unused+members+during+updatesRe: keep values of model's unused members during updates <p>I think you'll find that most people use a hidden field. It isn't ideal, but if you want the data in your controller it has to be in the form somewhere.</p> 2010-04-02T12:21:35-04:003764325http://forums.asp.net/p/1543075/3764325.aspx/1?Re+keep+values+of+model+s+unused+members+during+updatesRe: keep values of model's unused members during updates <p>You could also use TempData for this. &nbsp;</p> 2010-04-02T12:26:10-04:003764380http://forums.asp.net/p/1543075/3764380.aspx/1?Re+keep+values+of+model+s+unused+members+during+updatesRe: keep values of model's unused members during updates <p>thanks guys,</p> <p>as i said&nbsp;the hidden field is very vulnerable , a malicious user could&nbsp;simply&nbsp;edit it's value and then make unauthorized changes</p> <p>TempData cause other issues, because it's not tied to current page, so if the user wants to make several changes in&nbsp;different&nbsp;tabs at the same time then boom!, everything goes wrong</p> <p><br> </p> 2010-04-02T12:54:21-04:003764390http://forums.asp.net/p/1543075/3764390.aspx/1?Re+keep+values+of+model+s+unused+members+during+updatesRe: keep values of model's unused members during updates <p>Very true, TempData has it's drawbacks. </p> <p>What you could do is make sure that the hiddenfield's contents&nbsp;is hashed in some sort of way, limiting the risc of tampering.</p> <p>Otherwise, i can't think of any other simple solution to this. </p> 2010-04-02T12:59:31-04:003764713http://forums.asp.net/p/1543075/3764713.aspx/1?Re+keep+values+of+model+s+unused+members+during+updatesRe: keep values of model's unused members during updates <p>Stick a [Bind(Exclude = &quot;CreatedByUserId&quot;)] attribute on the model type. &nbsp;This will prevent the binder from ever attempting to set that property. &nbsp;(This value will probably be default(TProperty) if you're creating a new object, or it will maintain its original value if you retrieved the model from the DB as part of this action.) &nbsp;When submitting this updated object to your repository, the repository would have to be smart enough to compare the previously stored ID with the current user ID.</p> <p>If you absolutely need to keep the CreatedByUserId around, you may want to consider sticking it in Session. &nbsp;As long as Session is stored at the server (the default configuration), it's tamper-proof by end users.</p> <p>Also, MVC Futures also has Html.Serialize() and the [Deserialize] attribute, both of which can be configured to encrypt &#43; sign the serialized contents. &nbsp;See <a href="http://blog.maartenballiauw.be/post/2009/10/08/Leveraging-ASPNET-MVC-2-futures-ViewState.aspx"> http://blog.maartenballiauw.be/post/2009/10/08/Leveraging-ASPNET-MVC-2-futures-ViewState.aspx</a>&nbsp;for more information. &nbsp;The Sign &#43; Encrypt parameter to these methods prevent inspection of and tampering with the generated data, but the data can still be replayed. &nbsp;(You may also serialize a timestamp in the data to create a window after which replays are invalid, if you wish.)</p> 2010-04-02T17:02:03-04:003764773http://forums.asp.net/p/1543075/3764773.aspx/1?Re+keep+values+of+model+s+unused+members+during+updatesRe: keep values of model's unused members during updates <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sos00</h4> suppose our model has a property named &quot;CreatedByUserId&quot; that keeps the creator's user id</blockquote> <p></p> <p>if you are using some sort of FormAuthentication then best will be <b>User.Identity.Name</b></p> <p><br> </p> 2010-04-02T17:41:38-04:00