Nani
Nani

Reputation: 9

How to disable a textbox while when we hit edit option in edit.cshtml page in mvc?

====> Here is my edit.cshtml page code:

         <div class="form-group">
        @Html.LabelFor(model => model.Eno, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Eno, new { htmlAttributes = new { @class = "form-control" } } )
            @Html.ValidationMessageFor(model => model.Eno, "", new { @class = "text-danger" })
        </div>
    </div>

I want to disable this textbox. Can anyone help me with that.

This is my Controller:

    [HttpGet]
    public ActionResult Edit(int id)
    {
        Models.Employee e1 = new Models.Employee();
        e1.Eno = id;
        e1 = objdalemp.SearchEmp(e1);
        return View(e1);
    }

    [HttpPost]
    public ActionResult Edit(Models.Employee e1)
    {
        int i = objdalemp.UpdateEmployee(e1);
        if(i==1)
        {
            return RedirectToAction("Index");
        }
        return View(e1);
    }

Upvotes: 0

Views: 82

Answers (2)

Tej
Tej

Reputation: 414

You can disable a html field generated using the following

@Html.EditorFor(model => model.Eno, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } }).

Upvotes: 3

TechGuy
TechGuy

Reputation: 4560

You can disable like below.

@Html.EditorFor(model => model.Eno, new { htmlAttributes = new { @disabled ="true", @class = "form-control" } })

Upvotes: 2

Related Questions