mvc4 html.hidden,asp.net mvc - MVC3 Html.HiddenFor(Model => Model.Id) not passing back to Controlle...

I have created a strongly typed MVC3 Razor view using the scaffolding code.

The model is a POCO with a base type of PersistentEntity which defines a property called Created, Updated and Id.

Id is an int, Created and Updated are DateTime.

I am using Html.HiddenFor to create the hidden field on the view.

@Html.HiddenFor(model => model.Id)

@Html.HiddenFor(model => model.Created)

@Html.HiddenFor(model => model.Updated)

On the page, the hidden input is being rendered properly, with the Id being set in the value.

However when the page is submitted to the controller [HttpPost]Edit(Model model) the Id property is always 0. Created and Updated are correctly populated with the values from the View.

oIpSJ.png

This should be 12 in the case of the example in this post. What is going wrong?

I am aware that I can change the method signature to [HttpPost]Edit(int personID, Person model) as the personID is in the get string, however why does the model not get populated with the hidden field?

Update

The problem was that the setter on PersistentEntity was protected, ASP could not set the property, and swallowed it. Changing this to public has solved the problem.

public abstract class PersistentEntity

{

public virtual int Id { get; protected set; }

public virtual DateTime Created { get; set; }

public virtual DateTime Updated { get; set; }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值