I am using razor view engine(cshtml) outside of mvc framework. i.e only view is converted to cshtml format.
I make all database calls using webmatrix and construct cshtml view.
Although, now i want to fire dropdown list selected change event which will set or fill contents of another dropdown list on the same page.
Currently I use dropdown list using
#HTML.DropdownList("ID",List)
on the selectedindex change of dropdown, i wish to check it's value and then decide on whether or not to populate second dropdown.
How can I do it in this scenario? Remember, no mvc framework, hence no access to models or mvc specific methods.
when the dropdown list is rendered in the browser, it simply becomes a HTML tag and you can use vanilla javascript or frameworks like jQuery to check the item selected and load the next dropdown.
Sample:
$( "#myselect option:selected" ).text();
Related
Here's my scenario:
I need to create a page such that I have a view containing a DropDownList, then another (partial?) view beneath it that changes depending on what was selected in the DropDownList. I know how to code the DropDownList and make it work with the controllers, but I'm pretty stumped on how to achieve my goal.
The view that will change based on the DropDown has its own controller with CRUD operations (they contain grids). I should add that i'm using a shared view that contains a sort of template for a grid. There's lots of grids, but one view, and a controller for each grid. So I can't just call in the view as a partial view, as it would have no data. I'm essentially needing to call the controller for each grid, which then renders the view containing the grid.
So how can I do this?
you can use partial views to render your views, hence you can use Ajax to update the contents of any element or part of the rendered views dynamically.
you may use jquery to get the selected item value from the drop down list
I have a Database that contains several Booleans. I'm displaying this database in my View but I want the user to be able to change the value behind the checkbox.
Is it possible to define some kind of onChange or onClick event for Html Checkboxes in Razor? Kind of like an Action link, so I can call the corresponding Controller Action to change the value?
Preferably without having to use too much JS or AJAX
In My asp.net application (not MVC, conventional asp.net 4.0)..
I have a nested list object. The structure is
plantDTO = new PlantDTO();
plantDTO.LstTuningForks = new List<TuningForkDTO>();
tuningDTO = new TuningForkDTO();
tuningDTO.LstModule = new List<ModuleDTO>();
modDTO = new ModuleDTO();
modDTO.LstResourceTypes = new List<ResourceTypeDTO>();
restypeDTO = new ResourceTypeDTO();
restypeDTO.LstResource = new List<ResourceDTO>();
resDTO = new ResourceDTO();
restypeDTO.LstResource.Add(resDTO);
modDTO.LstResourceTypes.Add(restypeDTO);
tuningDTO.LstModule.Add(modDTO);
plantDTO.LstTuningForks.Add(tuningDTO);
I have values loaded in all these objects. the values are fetched from database and popuated. the idea is one plant may have multiple tuningfork, one tuningfork may have multiple modules, one module have multiple resource types and so on..
Now my requirement is i want to load 5 drop downs for
Plant, tuningfork, module, resourcetype, resource
in a cascading fashion.
i.e if one plant is selected then only its correponding tuningforks has to be loaded in tuningforks drop down,
again based on the selected tuningforks the module drop down has to reload with the respective data an so on
the values are available in the list object and i dont want to hit db
again
I require to do it using jquery as i dont want the page to post back
on selected index changed. More over i require to do it without using
[web method] attribute in my code behind.. Please let me know how to
achieve it..
I know you said you have requirements not to perform postbacks and not to use web methods. Why the arbitrary requirements? Because otherwise, the CascadingDropDown from the Ajax Control Toolkit fits your needs.
CascadingDropDown is an ASP.NET AJAX extender that can be attached to
an ASP.NET DropDownList control to get automatic population of a set
of DropDownList controls. Each time the selection of one the
DropDownList controls changes, the CascadingDropDown makes a call to a
specified web service to retrieve the list of values for the next
DropDownList in the set.
When the page initially loads, embed the hierarchy in your page's HTML as some form of data structure. Then have JavaScript that populates the drop downs according to the hierarchy, and JavaScript functions that handle the repopulating the other drop downs if there's a change.
How can I create a dynamic drop down list without using AutoPostBack. I ask because I just want to change the value of what the second drop down list displays and then the user clicks a button and it submits. But If I use AutoPostBack then it postbacks to page and runs code that shouldn't be run until that final box has been selected. (I use Postback in the other part of my program so using !IsPostBack isnt a option.) I looked at Javascript but ASP generates all of its controls names at runtime. What can I do? I have looked at the Ajax CascadingDropDown control but my problem with that is it contained in a XML file or a Database, I need this to be contained inside my Page. Any Ideas?
You can use the CascadingDropDown control from the AJAX Control Toolkit
Maybe this example will help? It's part of the ASP.NET AJAX Control Toolkit available here.
You can use AJAX to get the values for the second drop down list, based on the selected value of the first. Add a onchange event handler on the client-side to the first drop down list that makes the AJAX call and fills the second on success.
Am I able to use asp:Repeater and Html.ActionLink together for creating a dynamic menu? Or is there any other methods that I can use it?
Note: I'm getting the menu list from SQL.
I'm going to assume you are using ASP MVC 1 or 2 if you're using HTML.ActionLink. If that is the case, what you'll want to do is pass your list of items to the view through your Model or ViewModel and in the view, create a for each loop to display the items instead of using a repeater control.
Another option is to create a partial view that you pass your list of menu items to and create the for each loop in there then render the partial where ever you need to show your menu.
if you put the menu in the Master Page it will automatically show up every where.
I think you can, there are a lot of information on the web about how to use asp controls in MVC code. also, MVC seems to have its own repeater: http://davidhayden.com/blog/dave/archive/2009/04/07/ASPNETMVCControlsASPNETMVCFuturesRepeaterControlExample.aspx