asp.net mvc 3 - DisplayFor is not keeping data in ViewModel on PostBack -
this question has been asked before other, still don't it. using mvc3 razor , have screen entering trouble tickets. once data (a textarea note) entered, controller redirects same screen. of data display only. if use displayfor or displaytextfor, data not being posted back. have used hiddenfor. works. however, keep hearing others hiddenfor not ideal. don't want editorfor because, want disable field. (i follow working says hiddenfor wrong, won't why. :< lol)
razor
@html.displaytextfor(m => m.ticket.name)
viewmodel
public class ticketdetailsviewmodel { [displayname("customer name")] public string name { get; set; } control [httppost] public actionresult detail(ticketdetailsviewmodel viewmodel) return redirecttoaction("detail");
only values part of input or other form control posted client. displayfor meant model display data, nothing else.
you have 2 options; use hidden field model populated data each time, or fetch value again. former method fine if data isn't sensitive because end user can change hidden value , repost form. example unitprice not you'd want ever client in hidden field malicious user can modify unit price. in case you'd want reload data , repopulate model.
there's nothing wrong either; if data isn't editable , need display it, need every time. i'd err on side of caution , refetch read-only data each time on server , not put data hidden fields.
Comments
Post a Comment