A.B.
A.B.

Reputation: 2470

ASP.NET MVC: How to bind an object property to an HTML attribute

Here is my dummy model:

class Customer {
    string Name;

    // View Model part
    bool IsDisabled;
}

Here is my view:

@using (Html.BeginForm(actionName, controllerName))
{
   @Html.TextBoxFor( c => c.Name, new { disabled = ***) })

   <input type="submit" value="Submit" />
}

So the question is: How can i bind the 'isDisabled' property of my viewmodel to the 'disabled' attribute of my textbox? i.e. What should stand in the place of '*' ?

Upvotes: 1

Views: 367

Answers (2)

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

@Html.TextBoxFor(c => c.Name, Model.IsDisabled ? new { disabled = "disabled" } : null)

Upvotes: 2

user3497034
user3497034

Reputation:

@Html.TextBoxFor( c => c.Name, new { disabled = c.IsDisabled) })

Upvotes: 1

Related Questions