1

In my HTML file, I have a textbox which must be disabled or enable, depending on my controller value. No problem to set it in disabled mode, but to set it enable...

this is my code :

<%= Html.TextBoxFor(model => model.test, new Dictionary<string, object> { { "disabled", ViewContext.RouteData.Values["controller"].ToString() == "MyTest" ? "" : "disabled"}}

I have seen some ideas on this question : here

mvccontrib.FluentHtml or InputExtensions are the single solutions, to answer to my question ???

Im using "disabled" but I can use "read-only" attribute... the goal of this code is not to allow the user to fill the text box...

Thanks for your advices on the subject.

4 Answers 4

2

Just break the line apart into something like this:

<% 
if (MyConditionIsTrue) 
    Response.Write(Html.TextBoxFor(model => model.test, new { disabled = "true" }));
else 
    Response.Write(Html.TextBoxFor(model => model.test));
%>
Sign up to request clarification or add additional context in comments.

1 Comment

Hello guys. Thanks for your answers. I suppose the 3 are goods, but Johannes's is the more simple to applicate.
2

That's a good candidate for a custom HTML helper:

public static class HtmlExtensions
{
    public static MvcHtmlString CustomTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex)
    {
        var controller = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
        var htmlAttributes = new Dictionary<string, object>();
        if (string.Equals(controller, "MyTest", StringComparison.OrdinalIgnoreCase))
        {
            htmlAttributes["disabled"] = "disabled";
        }
        return htmlHelper.TextBoxFor(ex, htmlAttributes);
    }
}

and then:

<%= Html.CustomTextBoxFor(model => model.test) %>

2 Comments

Perfect example for htmlhelper creation which uses Model.
it seems to be a part of code interessant, Im not used to program. Where do I need to paste this code ? I mean, which folder ?
0

People love Html helpers, but you don't have to use them.

@if (MyConditionIsTrue) {
  <input id="test" name="test" value="@Model.test" disabled="disabled" />
}
else {
  <input id="test" name="test" value="@Model.test" />
}

If you have to reuse this logic many times, a html helper is probably a good idea. If you're just doing it once, it might not be.

3 Comments

I would like to understand, why people dont have to use Html Helpers ?
By the way, the "@" is not from the initial language of MVC, no ?
Or <input name="test" value="@Model.test" @(MyConditionIsTrue ? "disabled" : "") />
0
disabled
or
disabled = disabled
or
disabled = true
or
disabled = false
or
disabled = enable

all of the above means disabled.

remove disabled from the element to enable that.

if(condition)
   <input .... disabled/>
else
   <input ..... />

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.