0

I'm developing an ASP.NET MVC 5 app with Razor, C# and .NET Framework 4.7.

I want to make it a textbox not editable if Model.IsChinaProduct is true.

I have this piece of code in a View:

@Html.TextBoxFor(m => m.Configurations[index].PkgRatio, new { @class = "productClass", @onkeydown = "IsValidKey(event);" @if (Model.IsChinaProduct) disabled})

I want to add the disabled attribute if Model.IsChinaProduct is true, but that code shows me the following error:

Error CS0746 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

How can I add the disabled attribute if Model.IsChinaProduct is true?

UPDATE:
Maybe disabled is not the right attribute.

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 1
    Do you really want it disabled (disabled inputs do not submit a value)? Since you have other attributes as well, then refer [this answer](https://stackoverflow.com/questions/34889537/conditional-html-attribute-with-html-helper/34889685#34889685) for an example –  Sep 13 '17 at 08:22
  • You are inside a function, creating an anonymous type.. `if` statements are not valid in an anonymous type declaration. You probably just want the disabled property set to true or false. – Sam Axe Sep 13 '17 at 08:22
  • @StephenMuecke Sorry, no. I want to make it not editable. Sorry. – VansFannel Sep 13 '17 at 08:25
  • @VansFannel Then you need to use `readonly` attribute instead for non-editable textbox. You can perform the same way as duplicate link above but with `readonly = "readonly"` attribute. – Tetsuya Yamamoto Sep 13 '17 at 08:26
  • The what you want is a `readonly = "readonly"` attribute –  Sep 13 '17 at 08:26
  • Refer this :'https://stackoverflow.com/questions/6660146/set-disable-attribute-based-on-a-condition-for-html-textboxfor' – Natarajan Nagarajugari Sep 13 '17 at 08:34

3 Answers3

0

AFAIK you can't, because there is no disabled="false", meaning you should do something like that:

@{
    var htmlAttributes = Model.IsChinaProduct ? (object)
        new { @class = "productClass", readonly = "readonly" }
        : new { @class = "productClass", @onkeydown = "IsValidKey(event);" };
}
@Html.TextBoxFor(m => m.Configurations[index].PkgRatio, htmlAttributes)
Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • That will not work - you need to cast to `object` - `var htmlAttributes Model.IsChinaProduct ? (object)new { ... } : (object)new { ... }` –  Sep 13 '17 at 08:25
  • @StephenMuecke Thanks, fixed. I always forget that until the compiler reminds me ;-)... – Christoph Fink Sep 13 '17 at 08:27
0

For setting it ReadOnly, try this:

@{
   object displayMode = (Model.IsChinaProduct) ? new { @class = "productClass", @onkeydown = "IsValidKey(event);" } 
                                               : new { @class = "productClass", @onkeydown = "IsValidKey(event);" readonly = "readonly"};
   @Html.TextBoxFor(m => m.Configurations[index].PkgRatio, displayMode)
}
SᴇM
  • 7,024
  • 3
  • 24
  • 41
0

Dont use TextBoxFor if IsChinaProduct = true Try DisplayFor combine with HiddenFor instead

like this

@if (Model.IsChinaProduct)
 {
    @Html.HiddenFor(m => m.Configurations[index].PkgRatio)
     @Html.DisplayFor(m => m.Configurations[index].PkgRatio)
 }
 else
 {
     @Html.TextBoxFor(m => m.Configurations[index].PkgRatio)
 }
Dan Nguyen
  • 1,308
  • 6
  • 17