1

I need to hide/show some controls in cshtml based on what is selected in a dropdown list. I want to be able hide/show these sections before the value of the dropdown list is sent back to model and saved. How do I do this? I looked at these questions but had no luck question1 question2

@model Models.EmployerModel
@{ ViewBag.Title = "Index"; }
<div>
<p class="validateTips">
    Enter the new employment details.</p>
<fieldset>
    <table>
        <tr>
            <td>
                <label>
                    <b>Employment Type:</b></label>
            </td>
            <td>
                @Html.DropDownList("EmploymentTypeDrp")
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    <b>Job Title:</b></label>
            </td>
            <td>
                @Html.TextBoxFor(m => m.JobTitle, new { Value = @Model.JobTitle })
            </td>
        </tr>
    </table>
</fieldset>
</div>

So what I need is that based on what is selected in EmploymentTypeDrp, Job Title be visible or not. I have a JQuery bit to update the model based on new information.

2
  • 2
    You need javascrpt/jquery to respond to client side events (handle the .change() event of the dropdownlist and show/hide the elements). But showing/hiding them does not change what is posted back to the controller so not sure what your trying to achieve. Commented Feb 11, 2016 at 4:34
  • 2
    Side note: remove new { Value = @Model.JobTitle } - the value is already set by the TextBoxFor() method and you should never try and override the value attribute Commented Feb 11, 2016 at 4:53

1 Answer 1

2

Can you please try this

Your view

@model Models.EmployerModel
@{ ViewBag.Title = "Index"; }
<div>
<p class="validateTips">
    Enter the new employment details.</p>
<fieldset>
    <table>
        <tr>
            <td>
                <label>
                    <b>Employment Type:</b></label>
            </td>
            <td>
                     @Html.DropDownList("EmploymentTypeDrp", new List<SelectListItem>

             {
                new SelectListItem{ Text="Admin", Value = "1" },
                new SelectListItem{ Text="HR", Value = "0" }
             }) // I just hard coded the departments here                
          </td>
        </tr>
        <tr>
            <td>
                <label>
                    <b>Job Title:</b></label>
            </td>
            <td>
                @Html.TextBoxFor(m => m.JobTitle, new { Value = @Model.JobTitle })
            </td>
        </tr>
    </table>
</fieldset>
</div>

In the script section of your view you can use .change() event as suggested by @Stephen Muecke

<script>
$(document).ready(function () {
  $('#EmploymentTypeDrp').on('change', function (e) {

        var selectValue = this.value;
        if (selectValue == "Admin") {
            $("#JobTitle").show();
        }
        else {
            $("#JobTitle").hide();
        }

    });
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.