I want to display a property as read only on a MVC view, and when the view is HTTP posted back, the property value remains, rather than null.
I tried the code, below but model binder return null,
@model Car
@Html.DisplayFor(x => x.Name); //need to be read only, but returns the value on HTTP POST
public class Car
{
public string Name { get; set; }
}
public ActionResult Index()
{
var car = new Car() {Name = "1"};
return View(car);
}
[HttpPost]
public ActionResult Index(Car car) **//Name is NULL**
{
return View(car);
}
Thanks in advance!
How about adding
you can still put the
@Html.DisplayFor(x => x.Name)on the page for display purposes.