Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to give enable and disable for @html.textbox in controller in mvc3.

My textbox code:
HTML
@Html.TextBox("txtIden1")


Thanks.
Posted
Updated 14-Jun-12 20:46pm
v4

The disable property isn't true \ false, but should just read as disabled....e.g

C#
@Html.TextBox("txtIden1", new { disabled="disabled" })



You don't set this in a controller, your controller is responsible for returning a model to the view that dictates how it should be rendered. So, in your controller method

C#
// Your model
public class SomeModel 
{
    public string SomeName {get; set;}
    public bool FieldIsDisabled {get; set;}    
}

// Your controller action
public ActionResult GetMyModel()
{
    var model = new SomeModel
    {
        SomeName = "Test", 
        FieldIsDiabled = true // would actually be some logic to determine this...
    }

   return View(model);
}

// Your view page

@model SomeModel
@{
    ViewBag.Title = "Home Page";
}


@Html.TextBoxFor(model => model.SomeName, model.FieldIsDiabled ? (object)new { disabled="disabled" } : new {})
 
Share this answer
 
v2
Comments
sampath1750 12-Jun-12 6:25am    
How can i give disabled and enabled property in controller
Dylan Morley 12-Jun-12 6:39am    
See my updated answer
sampath1750 12-Jun-12 7:55am    
But i didnt use Model, button click event only i wrote the textbox code
Dylan Morley 12-Jun-12 7:58am    
I'm not entirely convinced you understand how MVC works. Why not try the example I've posted, see if you can get it working...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900