0

I have a @Html.DropDownList that I need to have it's text and value changed based off of a prior choice.

if ($(data.agt.CoAgentMLSAID != null) == "True") {
$("#SelectedAgent").html("{0}, {1}", data.agt.CoAgentLastName,
data.agt.CoAgentFirstName);
}

I have also tried this:

$document.getElementById("#SelectedAgent").text = ("{0}, {1}", data.agt.CoAgentLastName, data.agt.CoAgentFirstName);

instead of the other one above. Didn't work

And I have this:

@Html.DropDownList("SelectedAgent", this.Model.OfficeAgents, new { @class = "fl-space2", @id ="SelectedAgent" } )

What I'm getting is the default value and not the value of the names based off of the if statement above. How can I change that? The default text is in the Model.

I'm not very strong with jQuery, so I'm betting it's something quick. Thanks in advance

1 Answer 1

1

Try following code: Create view model as below:

public class ViewModel{

  public List<SelectListItem> OfficeAgents {get; set}
  public string SelectedAgent {get; set}

  public MyViewModel(){
      OfficeAgents = new List<SelectListItem>();
      SelectedAgent = "ABC"; //default values go here
  }

}

Send this view model to ur view:

public ActionResult Index()
{

    ViewModel model = new ViewModel ();

   //do query and set agents data to model.OfficeAgents and set default agent data

    model.SelectedAgent = "Agent1";

    //Pass it to the view 
    return ActionResult( model);
}

Set ur model to dropdownlist in view:

@Html.DropDownList("SelectedAgent", this.Model.OfficeAgents, this.Model.SelectedAgent, new { @class = "fl-space2", @id ="SelectedAgent" } )
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.