I have been struggling with this all day today. I even dont want to use jquery or javascript to solve this problem. I only need to use controller to make it work. So I have a dropdownlist (List: ManagerName, EmployeeName).
When selecting "ManagerName" from the dropdownlist, the categorie value will pass to a parameter of ChangeTextBoxes Method in Controller (working for me) but I dont know how to disable textbox in Index.cshtml and enable them in controller. Like this bottom code.
If any of you strongly suggest me to use javascript or jquery, I need a very simple code (good example) from stratch to end so that I can understand better.
Index.cshtml
@using (Ajax.BeginForm("ChangeTextBoxes", "Home", ajaxOptions: new AjaxOptions
{
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "searchResults"
}))
{
@Html.DropDownList("categorie", new SelectList(new[]
{
"ManagerName", "EmployeeName"
}) as SelectList)
@Html.TextBox("txtManager") //how to disable or invisible?
@Html.TextBox("txtEmployee") //how to disable or invisible?
<input id="Submit" type="submit" value="submit" />
}
HomeController.cs
public PartialViewResult ChangeTextBoxes(string categorie)
{
switch (categorie) //get selectedvalue from dropdownlist which is working
{
case "ManagerName":
//how to enable txtManager (textbox) ???
break;
case "EmployeeName":
//how to enable txtEmployee (textbox)????
break;
}
continue .......
}
I really hope any of you can help me solve this problem. It would be something I learn and understand how it works. I have a very little knowledge of MVC but it is a challenge to learn it. Thanks so much for your time!!
Update
Inside Index.cshtml
@Html.DropDownList("categorie", new SelectList(new[]
{
"ManagerName", "EmployeeName"
}) as SelectList)
@Html.TextBox("txtManager")
@Html.TextBox("txtEmployee")
<script type="text/javascript">
$('#categorie').on('change', function () {
if ($(this).val() == "condition to disable") {
$('#txtManager').hide(); //invisible
$('#txtManager').attr('disabled', 'disabled'); // disable
} else {
$('#txtManager').show(); //visible
$('#txtManager').removeAttr('disabled'); // enable
}
});
Why it is not working? Do I miss something or do wrong?