Asp .net MVC 3 application... This is the View:
Grupa: <%= Html.DropDownListFor(x => x.Grupa, Model.ListaGrupe) %>
Produsul: <%= Html.DropDownListFor(x => x.Produs, Model.ListaProduse) %>
Cantitate: <%=Html.TextBoxFor(x => x.Cantitate, new { style = "width: 100px;" })%>
Pret: <%=Html.TextBoxFor(x => x.Pret, new { style = "width: 100px;", disabled = true})%>
TVA: <%= Html.TextBoxFor(x => x.TVA, new { style = "width: 100px;", disabled = true })%>
Valoare: <%= Html.TextBoxFor(x => x.NoTVA, new { style = "width: 120px;", disabled = true})%>
Valoare cu TVA: <%=Html.TextBoxFor(x => x.Total, new { style = "width: 120px;", disabled = true})%>
I am using some JQuery to change Pret, TVA, NoTVA and Total based on the values in Grupa, Produs and Cantitate so I don't want the user to modify the values inside them. Probably disabled = true shoudn't be used. Then how can I make so the user can't modify the fields but the value to be posted to the controller's action?
Well, this is what i did up to now,
i didn't succeed to make a good, easy to use, readonly protection using encryption,
but i did manage to do something that i think might just do.
how it works:
When you use
LockObject(o)
an object, itterate the properties that have definedProtectedAttribute
defined for.add the locked value to a list, specially made for this field.
! the list is kept in the user session (on the server side)
when the user submits the form,
IsValid
checks to see if the value is in the list of locked values. if yes, then it is all ok. otherwise, it must have been changed somehow.! the number of values is not that big, and is temporary to the session, but if it is bothering someone, a simple
lockList.remove(node);
can easly be added when a value is validated.Note: this can cause problem when the user uses Back buttons or Resubmit a form using Refresh.
tell me if you find any problems that this model does not take into account...
+ the Equalization is very naive, so it works only with value-types for time be.
Code:
Created an attribute named
ProtectedAttribute
:place this attribute on any property of your model just as any other validation.
in the controller, after you are finished with the viewmodel object, you call
ProtectedAttribute.LockObject(myViewModel)
You can use Html.HiddenFor() and use a
<span>
or<div>
instead. Their values will then be posted back.You can also make them readonly rather than disabling them. On the other note, I think @Chris solution is better, that way your modified data will be posted back.