I am currently trying to take a value from a drop down list, store it into a variable, and pass it into a controller's action result. When I run my code, the Index is supposed to store the value of the selected item in the dropdown box in the variable SelectedDistrict, then then go to the Query action. Query will take a variable of DistrictViewModel as a parameter and then use var school = getQuery(variable.SelectedDistrict) to go into the function I have. In the function however, it's saying that the variable sd is null whenever i debug. Maybe the value from the drop down box is not storing properly? In the end, I want to display in a table all of the schools in my school table that come from the selected district in the drop down. The table is not being populated because of the null value. Here is my code for more clarity.
District View Model:
School View Model:
Controller w/ getQuery function:
Index View:
Query View:
The table when I run my code:
The problem is that your controller method for Query is expecting some data to be passed in the form of an object, but you aren't passing anything to it. If it's a GET request (and it appears to be because you just set location.href on click), the values would need to be in the query string. Alternatively you can make your form POST to that controller action instead.
You'll need <form> tags. You can GET or POST to your controller method, it won't matter (model binding works either way). It depends on whether you want people to be able to deep-link directly to the search results or not.
<form action="#Url.Action("Query", "District") method="get">
#Html.DropDownListFor(m => m.SelectedDistrict, Model.Districts)
<button type="submit" value="Submit"/>
</form>
That pretty much should do it, or at least get you on the right path.
Related
My goal is very simple and I would guess it is a very common goal among web developers.
I am creating a Rails (5.1) application, and I simply want to use AJAX to get the value of a specific cell in a specific row of a specific table in my database (later I am going to use that value to highlight some text on the current page in the user's browser).
I have not been able to find any documentation online explaining how to do this. As I said, it seems like a basic task to ask of jquery and ajax, so I'm confused as to why I'm having so much trouble figuring it out.
For concreteness, say I have a table called 'animals', and I want to get the value of the column 'species' for the animal with 'id' = 99.
How can I construct an AJAX call to query the database for the value of 'species' for the 'animal' with 'id' = 99 .
Though some DBs provide a REST API, what we commonly do is define a route in the app to pull and return data from the DB.
So:
Add a route
Add a controller/action for that route
In that action, fetch the data from the DB and render it in your preferred format
On the client-side, make the AJAX call to that controller/action and do something with the response.
The app I am working on has hidden input variables set on initial load of the page. The variables need to be accessed across all pages. The reason for not converting these to session variables is the user can open another browser with different parameters and passing those value as hidden inputs make it work without any issue. What is the best possible alternative so as to get rid of these hidden inputs..I tried converting all variables into a Structure, but again the structure needs to be posted as part of form submission to make it available to subsequent pages.Another disadvantage of this app is use of frames.I don't have any code to post.
It sounds as if you want different browsers instances which are associated with the same web session to maintain their own distinct sets of data. Doing this by passing form or url variables around seems like a bad idea.
One approach that you could use would be, onSessionStart (or as required), create a structure in the users session to hold instances of the data. For example
session.data = {
someRandomKey: {
valueA: 42,
valueB: "Porridge"
}
}
Then just pass someRandomKey as a hidden form field or querystring parameter. Now, when they submit the form to update variables you use the id from the hidden form field to find the appropriate structure from session.data.
When the user needs a new instance of this form, give them some way, like a link or button, that creates a new unique key, inserts a struct in session.data with this key and populates it with whatever defaults are needed then loads the form passing along the new id as a hidden form field or querystring param again.
I have two controls in a form.
zip code - (input type text) and city - (select options)
The back-end rest service sends json object which will contains zip codes and city names. I want to change city when zip code changed and vice versa using angularjs1. Also validate zip code with json object.
please help me...
Alright. So I am assuming that you have a serverside script from which you can get both city from pincode and pincode from city
Now you have got 2 controls in your view.
1) Input Box
2) Combobox
Now The easiest way according to me is that you should make a function that is called on ngChange event of both the controls.
That function would make a request to your server and fetch the associated data and feed the data to the other control.
Now when you populate the controls internally through the controller, it might again fire an ngChange function. So in each ngChange function you must have a reference of the desired values for them. That can be done by storing both the latest values received from server side into a local variable and checking it each time the function is called.
I believe this would solve your problem.
----------------Update----------------
As mentioned in the comment, the SPA shall not be sending request to the server during the execution. It shall simply send the json at the end.
So for that its more convenient. Simply make 2 different functions, 1 getCityFromPincode(pincode) and 2 getPincodeFromCity(city)
Now you can easily do your operations in javascript on the basis of the values that you will get from the ngChange() function.
Just call the function getCityFromPincode in the ngChange function of your CITY INPUT Control and similarly for the other one.
You would probably make it through with this approach.
I am trying to get either a dropdown or selection box(prefer the later, because of the possibility to choose multiple values at once) in a webform.
In this case i already got the dropdown working, based on measures.measurement_type. The second needs to be measures.measurement, filtered by the type selected in the first dropdown.
I can not seem to get this working. I tried googling, but without succes. Can anyone help me get on the right track?
I found solutions using Arrays, but no working solution using 1 database table.
using ruby 4.2
Thanks
There are two options for this.
Using AJAX calls. Like #Ronan said in his answer, you need to make an AJAX call on the selection of first drop down (on change method). In the rails action method, you can render a JS partial where you can set the filtered items for second drop down.
Another one is totally client side. Like render all the possible items of both drop downs to client side. Meaning both type and measurements as javascript array. Then when changing the type drop down, filter the measurements array using jQuery and populate in the second drop down.
You gotta use some ajax for doing that, can't see other way. When your measures.measurement_type changes, you send a request passing that measurement_type as param for your action. In that action, you retrieve the collection of measurements based on the measurement_type passed in the param, and then return that data to be handled on your success ajax callback. On that method, with some jquery you should populate the second input with the options returned.
This is some simpler explanation... you should take some look at a more complete article for understanding step-by-step. Would suggest this one, for example: https://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax. Good luck!
I have an mvc app where I want to assign a user to a profile. I have two multiline select lists (i.e <select size="10"></select>). One for profiles the user is part of and one for the available profiles the user can become part of.
I am using JavaScript with two buttons to move the items between the two lists. When the form submits the Request.Form object only knows about options that are selected in either list but I want to know all the items that are in the 'profiles the user is part of' list.
What can I do to acheive this? I was thinking of using a hidden field and placing the values in the field when they are moved to the 'profiles the user is part of' list. (i.e. <input type="hidden" name="user_profiles" values="3,8,12">).
What do you think? Is there a better way to do this? Thanks.
You could have a script to select everything in the list on the submit event.
Pass in the FormCollection to the action. Then update the model and store the data.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveProfile(FormCollection collection){
// collection is a key based collection so pass the key or the control name to get
// its value
Profile p = new Profile();
p.UserId = collection["UserId"];
p.Name = collection["Name"];
p.Birthdate = collection["Birthdate"];
p.ProfileOption = collection["ProfileOption"];
// DB logic should be done in a service but this is a very simplified example
profileRepository.Update(p);
return View();
}