1

This is what I tried:

  @Html.TextBoxFor(model => model.EntryDate, new { style="width:90px;",((_new || _modify) ? @disabled="disabled":"") }) @Html.ValidationMessageFor(model => model.EntryDate)
3
  • Cant you just use @if(comeCondition) { @Html.TextBoxFor(model => model.EntryDate) } else { @Html.TextBoxFor(model => model.EntryDate, new { @disabled = "disabled") } Commented Sep 1, 2014 at 12:03
  • Well I was hoping there is a simple and elegant way to to it Commented Sep 1, 2014 at 12:04
  • OK, will post answer Commented Sep 1, 2014 at 12:32

2 Answers 2

2

The following will work because there is only one html attribute

@Html.TextBoxFor(m => m.EntryDate, (_new || _modify) 
  ? new { @disabled = "disabled" } 
  : null)

However once you add the style attribute, this will generate an error because there is no implicit conversion between new { @disabled = "disabled", @Style="width:90px;" } and new { @Style="width:90px;" } so you need to do this in an if block

@if(_new || _modify) {
  @Html.TextBoxFor(m => m.EntryDate, new { @disabled = "disabled", style="width:90px;")}
} else { 
  @Html.TextBoxFor(m => m.EntryDate, new { style="width:90px;" })
}

Edit

Casting the html dictionary to object seems to work but I've not fully tested this (and I'm not sure that its "a simple and elegant way to to it")

@Html.TextBoxFor(m => m.EntryDate, (_new || _modify) 
  ? (object)new { @disabled = "disabled", style="width:90px;" } 
  : (object)new { style="width:90px;" })
Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure but try this one:

@Html.TextBoxFor(model => model.EntryDate, (_new || _modify) ? new{ @Style="width:90px;", @Disabled="disabled"} : new{ @Style="width:90px;"})

1 Comment

Are you sure these anonymous types will be compatible? Did you test it?

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.