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

Time:01-30

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

         <div >
        @Html.LabelFor(model => model.Eno, htmlAttributes: new { @class = "control-label col-md-2" })
        <div >
            @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);
    }

CodePudding user response:

You can disable a html field generated using the following

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

CodePudding user response:

You can disable like below.

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