0

I have a page that serves two purposes, mostly viewing an entry in the database, but some of the fields are displayed in textfields/textareas because you can edit some values and then depending on the button you select at the bottom, you can update those entries. I especially like the textarea because of it's look/feel, naturally resizeable in the page, and comes with standard right-click options like SelectAll, etc. thx to browser support.

Anyway, one of those fields (shown in a textarea) I'd like to prevent from coming through upon postback because that value isn't going to be edited (I've even made it @readonly so you can't even edit the textarea).

How to accomplish preventing form postback for an input from the ASPX page? I know that is counter-intuitive in general, but I thought perhaps there's a way similar to the Hidden inputs. Also, I know that in the controller I could just wipe that field in the model before doing anything with the model submitted from the form, but I'm curious if there is some option from the page that I could do.

I'm using this syntax to define the textarea:

<%: Html.TextAreaFor(model => model.Key, new { @readonly = "readonly", @class = "readonly", cols = 62, rows = 15 })%>

EDIT:
I probably should have noted that one of the main reasons I need to do this is because one of the model fields may contain some HTML in it. This is an internal website and that field cannot be edited manually, so no one can input HTML, it is just a value in the database that comes from a license generation program. And so, when I use a TextArea in a form that is bound to a model field, ASP.NET throws an exception upon binding the TextArea data back to the model to send upon postback.

I saw the SO answers using [AllowHtml] and [ValidateInput(false)] attributes, one wasn't available to me in MVC2 and the other requires downgrading the requestValidationMode, so I am looking for an alternative way to prevent the possible HTML value from being posted back at all (but still displayed in a textarea).

4

1 回答 1

0

我通过在 jQuery 中处理表单提交事件来实现这一点:

<script type="text/javascript">
    $(document).ready(function ()
    {
        // clear key area upon form post, because old keys contain HTML tags,
        // which cause an exception during model binding when form validation is on
        $("form").submit(function()
        {
            $("#keyArea").val("");
        });
    });
</script>

基本上只是在文本区域绑定到模型并发布之前清除它。这样我仍然可以使用 textarea 来显示模型值,并简单地防止它返回帖子。

唯一的小问题是,如果帖子将用户分流到另一个页面(可能),那么当用户按下返回按钮时,文本区域将为空白。但是一个简单的页面刷新解决了这个问题。

于 2013-06-04T13:54:15.840 回答