Monday, 26 August 2013

DisplayFor is not keeping data in ViewModel on PostBack

DisplayFor is not keeping data in ViewModel on PostBack

This question has been asked before by other, but still don't get it. I am
using MVC3 Razor and I have screen for entering trouble tickets. Once data
(a textarea note) is entered, the controller redirects back to the same
screen. Most of the data is for display only. If I use DisplayFor or
DisplayTextFor, the data is not being posted back. I have used HiddenFor.
That works. However, I keep hearing from others that HiddenFor is not
ideal. I don't want to editorfor because, I want to easily disable the
field. (I follow working says HiddenFor is wrong, but won't say 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");

No comments:

Post a Comment