Reputation: 83
I'm well versed in ASP.NET WebForms, and was considering switching my code to MVC 3.
I am learning MVC, and have some understanding in it, but, there's one issue I haven't come to understand yet.
It goes like this:
With WebForms code, I was able to have a DropDownList, that when selected, would've triggered a Button press through Javascript, and that button would've triggered a Method that would've populated another DropDown, but both DropDown's were part of the same form, and the actual Button that is visible to the user (and is to be manually pressed by the user) triggers a Method that gets values from both those DropDowns as well as other fields.
The thing is, I don't know how to do this with MVC. From my current understanding, if I wanted to send form data to the app, it has to follow a Model. That is what makes it so confusing to me (from my current understanding), how do I get the first DropDown to send only the data from inside the first DropDown? Also populating the other dropdown is a little bit of a foggy idea with MVC as well, but especially the point I made about the first DropDown is what makes me confused the most.
If anyone can help me in any way on this it would be greatly appreciated.
Upvotes: 2
Views: 154
Reputation: 83
I just remembered this question I asked a couple years ago. Another way would be to use $.get(...) with jQuery (which is well-documented nowadays...)
Upvotes: 0
Reputation: 53991
The big difference to get your head around here is that there are non of the old style postbacks in ASP.NET MVC.
In this case, you get to be in control of how you obtain the new data for your dropdown box.
When the form is complete and we're ready to send the data to the server, we'll be sending it back as a spcific type to our controller method but until that point, we can access the data we need from any other controller method.
Personally I'd use jQuery to do this using an ajax request to a controller method that returns a JsonResult
type.
Something like:
$.getJSON("/MyController/GetSecondDropdownValues", function(result) {
$.each(result, function() {
$('#mySecondDropdown').append($("<option />").val(this.Name));
});
C#
public JsonResult GetSecondDropdownValues(string initialDropdownValue)
{
/* Do the work to obtain the values here */
return Json(returnedValues);
}
Upvotes: 3
Reputation: 16358
Asp.net clasic follows a different mindset than asp.net mvc. The first tries to mimic a desktop forms application and abstracs (hides) the web platform details. Asp.net mvc while is a MVC based framework, it is actually a 'real' web framework because it doesn't try to hide anything web related from you.
That being said, the data the browser submits is actually 'data to update the Model' and not the Model itself. In most of the tutorials you'll see the View Model (or part of it) which is sent back, but it's just data submitted by the browser. There is a LOT of confusion of what the models is in an asp.net mvc application.
how do I get the first DropDown to send only the data from inside the first DropDown
You make that dropdown the only element of a form. In asp.net mvc you can have in a page as many forms as you need (as long as you don't mix them), asp.net mvc doesn't follow the form mindset of the webforms.
Alternately you can submit the value of the field via ajax (with jQuery) to a controller which returns a json that will be processed on the client side. And probably that's how you popoulate the second dropdown, when the first is selected, an ajax request is sent to an action of the controller which returns the content of the second dropdown as json, which is then inserted via javascript.
A simple hint to make you transition easier to asp.net mvc: anything view related (html - including forms, javascript etc) is independent from the controller. The View only cares about its (view) model, the controller cares about the data it receives. The controller returns a result which usually is a view , but could be anything. THe controller doesn't know anything about the view details (html, js), it knows only about the view data (the view model).
Upvotes: 0