开发者

ASP.NET MVC 2 Html.TextAreaFor Html.TextArea WEIRD Binding

开发者 https://www.devze.com 2023-03-11 05:16 出处:网络
This is the weirdest behaviour I have encountered in default set of controls/helpers in ASP.NET MVC I post a textarea value as such:

This is the weirdest behaviour I have encountered in default set of controls/helpers in ASP.NET MVC

I post a textarea value as such:

<%:Html.TextAreaFor(x=>x.Data.Body,new {style="display:none;", @class="ckeditor"}) %>

In my controller action I return a different value for Data.Body (i.e. model.Data.Body = "junk"), yet I still see the originally posted value inside the textarea box.

If I do

<textarea id="Data.Body" name="Data.Body" style="开发者_JAVA百科display:none;" class="ckeditor">
    <%=Model.Data.Body %>
</textarea>

It works as expected.

Any insight?

Thanks.


This is by design - the HTML Helpers will redisplay the posted data and not what is in the model. They assume you will use the Post-Redirect-Get pattern and that on a successful post you should redirect to a GET method. If you are redisplaying info after a post, the helpers assume there has been an error and they display the original posted values so the user can 'correct' them.

You can ModelState.Clear if you don't want this to occur, but I would consider programming around the standard PRG pattern of post-redirect-get.

I ran into this a while back - see my posting here: Updating value provider prior to TryUpdateModel

0

精彩评论

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