Inclusion of the second and third drop-down list depending on the value of the first drop-down list for MVC

I have three DropDownLists. If a specific value is selected from my first DropDownList, you need to include a second unpack list. For example, if "Player 3" was selected, the other two DropDownList should be enabled, although if "Player 2" was selected, the last DropDownList should be disabled and the second enabled.

How can i do this? First I use MVC 3 EF. This is my code, in my opinion:

 <p>Player</p>
     <div class="editor-field">
        @Html.DropDownListFor(m => m.PlayerName,Model.SubjectTypes, "Choose player" , new { @class = "selectstyle" })
        @Html.ValidationMessageFor(model => model.PlayerName)
    </div>

    <p>Position</p>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.PositionName, Model.Consultants, "Choose Position", new { @class = "selectstyle" })
        @Html.ValidationMessageFor(model => model.ContactPerson)
    </div>

    <p>Team</p>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.TeamName, Model.Teams, "Choose Team", new { @class = "selectstyle" })
        @Html.ValidationMessageFor(model => model.ContactPerson)
    </div>
+5
source share
2 answers

.change() , / :

$(function() {
    $('#PlayerName').change(function() {
        var value = $(this).val();
        if (value == 'Player3') {
            $('#PositionName, #TeamName').removeAttr('disabled');
        } else if (value == 'Player2') {
            $('#PositionName').removeAttr('disabled');
            $('#TeamName').attr('disabled', 'disabled');
        }
    });
});
+2

jquery:

$('#PlayerName').change(function() 
{
    var id = $(this).val()
    $.ajax({
        url: '@Url.Action("GetPositions")',
        data:
            {
                "id": id,
            },
        success: function (data)
        {
           $("#positions").html(data);
        }
    });
}

:

public JsonResult GetPositions(int id)
{
  var positions = Repository.GetPositionsByPlayerId(id);
  var hmtl = positions.Select(x => new SelectListItem
    {
      Value = x.PositionID.ToString(),
      Text = x.Name
    });
  return new JsonResult { Data = html };
}

, .

+3

All Articles