1

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">
                        <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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

Don't use HtmlString. Just use string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.