1

I have a Razor view with a couple of dropdown lists. If the value of one of the dropdown's is changed I want to clear the values in the other drop down and put new ones in. What values I put in depends on the values in the model that the view uses and that is why I need to send the model back from the view to the controller. The controller will then also need to be able to modify the dropdown by sending back data to the view. Note, that I am not saying that I want to go back to the controller from a form submit using Ajax. I am going back to the controller using a form submit, but not using Ajax.

Please can someone give me some simple code or some pointers to show how this might be done.

Thanks

1
  • Do I need to use partial views? Commented Feb 8, 2012 at 10:15

2 Answers 2

3

I personally use ViewBag & ViewData to solve this condition.

Controller:

public ActionResult Index()
{
    ViewBag.dd1value = default_value;
    ViewBag.dd1 = DropDownlist1();
    ViewBag.dd2 = DropDownlist2(dd1value);
    Return View();
}

View:

In the first dropdownlist add an onchange javascript.

<select onchange="javascript:this.form.submit();">
@foreach (var item in ViewBag.dd1) {
     if (ViewBag.dd1value = item.dd1value)
     {
         <option selected value="@item.dd1value">@item.dd1text</option>
     }
     else
     {
         <option value="@item.dd1value">@item.dd1text</option>
     }
}

Then, on submit button give it a name.

<input type="submit" name="Genereate" value="Generate" />

In the controller, create 2 ActionResult to receive data. For dropdownlist:

[HttpPost]
public ActionResult Index(int dd1value)
{
    ViewBag.dd1value = dd1value;
    ViewBag.dd1 = DropDownlist1();
    ViewBag.dd2 = DropDownlist2(dd1value);
    Return View();
}

For submit button:

[HttpPost]
public ActionResult Index(int dd1value, int dd2value, FormCollection collection)
{
    ViewBag.dd1value = dd1value;
    ViewBag.dd2value = dd2value;
    ViewBag.dd1 = DropDownlist1();
    ViewBag.dd2 = DropDownlist2(dd1value);
    ViewBag.result = Result(dd1value, dd2value);
    Return View();
}

If you don't need button:

[HttpPost]
public ActionResult Index(int dd1value, int dd2value)
{
    ViewBag.dd1value = dd1value;
    ViewBag.dd2value = dd2value;
    ViewBag.dd1 = DropDownlist1();
    ViewBag.dd2 = DropDownlist2(dd1value);
    ViewBag.result = Result(dd1value, dd2value);
    Return View();
}

Please note that if you use ViewBag / ViewData, all the help you get from the compiler is disabled and runtime errors/bugs will occur more likely than if the property has been on a "normal" object and typos would be catched by the compiler.

Sign up to request clarification or add additional context in comments.

Comments

3

I would implement a different solution from DragonZelda.

I would create a ViewModel object containing the data that you need on the page, that the View binds to.

Then, I would create controls that bind to that Model, like:

@Html.DropDownListFor(x => x.SomeDDLSelected, ......)

x.SomeDDLSelected would be a property in your ViewModel object that would automatically get the selected value in the dropdownlist when the automatic model binder gets in action.

Then, to finalize it, the Controller action would receive your ViewModel object as a parameter:

public ActionResult MyAction(MyViewModelObject obj)
{...}

And you get all your data nice and tidy, all strong typing.

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.