0

I have a HTML dropdownList which currently showing data from model, Now for some operations I want to change the dropdownList content. I guess If I update model it will reflect to view also. Below is my code for Model

ViewModel

public class CaseAssignmentRuleSetViewModel
    {
        public CaseAssignmentRuleSetViewModel()
        {

        }
       public int id { get; set; }
       public string name { get; set; }
       public Dictionary<string, string> AssignmentUsers { get; set; }
    }

So here in viewModel its a dictionary which is source for dropdownList in view which looks like below.

View

<select name="assignmentTeam" class="form-control case-assignment-use-type-value2" placeholder="Select a User">


            @foreach (string key in Model.AssignmentUsers.Keys)
            {
                <option value="@key">@Model.AssignmentUsers[key]</option>
            }
        </select>

and finally after some operations I am getting new list returned using JQuery function to my Controller method which looks like below.

Controller

 [HttpPost, Route("CaseAssignmentRuleSet/ListofAssignees", Name = "ListofAssignees")]
        public List<string> SetListofAssignees(string [] listOfAssignedroles)
        {
            CaseAssignmentRuleViewModel caseAssignmentRuleViewModel = new CaseAssignmentRuleViewModel();
            List<string> listOfAssignees = new List<string>();
            if (listOfAssignedroles!= null)
            {
                listOfAssignees = new List<string>(listOfAssignedroles);
            }

            return listOfAssignees;             

        }

How can I update my dropdownList in view with new list of items?

1 Answer 1

1

So when you say you are getting new list retunred using a jquery function, I am assuming what you are doing is making an ajax call to your controller.

If so then on your done function of the ajax call, clear the current list then iterate through your new list adding your new options: (pseudo code)

$.ajax( '/CaseAssignmentRuleSet/ListofAssignee' )
 .done(function(data) {

   //get the select element, you should really add an ID to the element to select instead of name but whatever
   var $el = $('[name=assignmentTeam]');

   //clear the current select list options
   $el.empty();

   //iterate through your new options and add them to the select list
   $.each(data, function(key,value) {
    $el.append($("<option></option>").attr("value", value).text(value));
 });
}).fail(function() {alert( "error" );}).always(function() {//alert( "complete" );});
Sign up to request clarification or add additional context in comments.

1 Comment

Actually I am not directly binding the list returned there is more processing on controller side. So I must have to bind processed data to dropdownlist.

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.