jeff
jeff

Reputation: 446

Is it possible to not render a hidden field in client side in mvc 3

In asp.net a hidden field can not be rendered in client side by having visible="false". Is it also possible in mvc 3 to not render a hidden field in client side?

Thanks in advance!

Upvotes: 0

Views: 247

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

You could put a condition:

@if (IsVisible)
{
    @Html.HiddenFor(x => x.Foo)
}

or write a custom HTML helper which will allow you to pass the condition to the helper directly:

@Html.MyHiddenFor(x => x.Foo, IsVisible)

which could be implemented like this:

public static class HtmlExtensions
{
    public static IHtmlString MyHiddenFor<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression,
        bool isVisible
    )
    {
        if (!isVisible)
        {
            return MvcHtmlString.Empty;
        }

        return htmlHelper.HiddenFor(expression);
    }
}

Upvotes: 2

Related Questions