开发者

Keeping Property value on HTTP POST, and is Read only on view, in ASP.NET MVC

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 Acti开发者_Python百科onResult 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

@Html.HiddenFor(x => x.Name)

you can still put the @Html.DisplayFor(x => x.Name) on the page for display purposes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜