开发者

Getting HtmlString value from form post in mvc3

开发者 https://www.devze.com 2023-03-23 12:21 出处:网络
i have a form ina razor view in my aspnet mvc3 project. The form, submits to a controller action that receives a Custom object that one of the properties is of type HtmlString.

i have a form in a razor view in my aspnet mvc3 project. The form, submits to a controller action that receives a Custom object that one of the properties is of type HtmlString. That property comes with null from the form submit and if the property is of type string, the value is the one the user writes. How can i get this value when the property is of that type?

Thanks!

Edit: The form is a create post.. what i mean is that is like this:

<form action="@Url.Action("SaveNote", "Application")" method="post" class="note-form" style="display:none">
        <fieldset>
            <div class="input_item">
                <div class="label">
                    <input type="hidden" name="urlRedirect" value="@Model.UrlRedirect" />
                    <select name="IsPrivate">
         开发者_JAVA技巧               <option value="False">Public</option>
                        <option value="True">Private</option>
                    </select>
                </div>
                <div class="input">
                    <textarea name="Body" style="width:290px; height:90px"></textarea>
                </div>
                <button class="link" type="submit" name="addnote" value="0" >save</button>
            </div>
        </fieldset>
    </form>

The Body is the HtmlString parameter that goes with null to the action.


HtmlString is simply a wrapper around a normal string to represent already Html encoded strings. So, unless you write a custom binder that creates an HtmlString object through it's constructor HtmlString(string value), you will get a null value for it.

The ToHtmlString() method merely returns the string passed in, atleast in the .NET 4 implementation.

So yes, just use string. It's functionally the same.


HtmlString should only be used in custom helpers when rendering some data on the view, but never use a property of this type on your view models. It simply doesn't make sense. Then inside your view based on whether you want to encode or not this string property simply use:

@Model.Foo

or

@Html.Raw(Model.Foo)


Don't use HtmlString. Just use string.

0

精彩评论

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

关注公众号