Using a ViewModel in a View in ASP.NET MVC app.
Have a dropdownlist on page and based on user selection, I need to show/hide a section. For certain options in DDL, the section is visible and for certain options the section is hidden.
For other similar features, I was able to create a property in ViewModel and call it in the View.
But in this current case, I need to pass the value of selection option and then find if it is one that needs the section hidden/visible.
Should I create a method in viewmodel and call method in view and pass the parameter of selected value? If so, then how to do that?
if that's not the way to do it then what's the recommended approach?
Thanks
You can bind a JavaScript/Jquery function to the dropdown list's onchange event. Inside the function you can write code to show/hide sections based on the selected value in dropdown list.
Further more, if you need to call a back end service to do any validations or retrieve data, you can use Ajax techniques.
Related
I would like to direct a user to a page within an app using a field value from a model. The model is loaded into a grid and the user makes a selection to navigate to a page within the app based on images displayed in the grid cells.
I am thinking to use the app.showPage(app.pages.Page) for the onClick event and passing in the page the user should be directed from the model as a parameter such as widget.datasource.item.PageName.
Can you suggest how the name of the page could be passed in from the datasource as as variable to the onClick event accomplish this navigation?
Will appreciate any help!
Lets say you are storing your page names in your datasource under a field PageName (string field) such as YourPageName1, YourPageName2, etc. Then for your onClick event all you would need is:
app.showPage(app.pages[widget.datasource.item.PageName]);
I'm creating a custom ASP.Net GridView and I want to be able to alter the __EVENTARGUMENT value but I can't figure out how to capture the returned value on the server side.
I'm creating the ability to have a collapsible representation, so the first level is the standard GridView and I will insert additional rows via JavaScript if they expand the first level row.
My problem is how to create a link on the selecond level rows that posts back with custom data.
I believe the RaisePostBackEvent is where this value gets passed into; the base GridView class allows you to override it, so you should be able to tap into it there.
This is a part of the IPostBackEventHandler interface.
HTH.
Use __dopostback to raise the postback from javascript. Use the correct parameters so that this will be mapped to the right event
Lets say I have three DropDownList controls in a web user control and they depend on each other.
Categories
Brands
Products
Explanation:
After I choose a category from Categories dropdown list, related brands are loaded in Brands DropDownList and same happens when I choose specific brand and they are all located in a web user control since I am using it too much on different pages, I don't want to copy and paste the same code on all the pages.
Problem: The pages can contain a GridView and DataSource control which needs an additional Where parameter to fetch all the data needed in and that parameter could depend on selected product within the Products DropDownList control.
Question: So how can I get that Selected Product Value from Products DropDownList to bind it to SQLDataSource or any other DataSource control.
My Thoughts: I belive I can solve this problem in the ways following.
I can use static variable which is updated once Products selected. That field variable could be public so everyone can reach it
Selected Products DropDownList can create a QueryString Field for me to grap the selected value.
In the same way, the dropdownlist can create a Session variable on the fly and I can fetch the value
It can create a hidden field maybe.
But: Well those are some of my thoughts but I found them so naive to implement. I need something elegant and satisfying to solve this problem. It should be something like a gateway from the Web User Control to outside world.
Maybe a separate class or a Property can help me in the gateway solution.
Anyways, I am waiting for your answers.
If I'm understanding the question correctly:
You can add a property to the user control that exposes the products DDL selected value.
You can also add and raise an event from the user control that fires when the products DDL selected value changes. Creating a custom event argument that contains the product value, allows it to get passed directly to the event handler.
Then your pages can handle the event raised by the user control, have the product value, and bind the grid.
You could bind the DropDownList.SelectedIndexChanged events to the same function, and test the SelectedValue properties of each DropDownList. If their SelectedValues are valid, bind the grid to your DataSource.
I've done this in the past when I needed users to input a certain amount of data before I could query the database. I set the Hidden property on my GridView if the DropDownLists weren't valid, and reset it when they were properly bound.
I have an "action" column in my repeater which shows actions a user can select for an item. The column contains ASP.NET HyperLink or LinkButton controls. Some actions are based on whether a user is in a role, which I determine programatically. I'm struggling with the best way to dynamically generate this column when I populate the repeater. Now I am assigning in-line code to the Visible property of each control, but I feel that is sloppy and not very straight forward. Would I be better served using a PlaceHolder control and populating that at runtime? What kind of methods do other people use for situations such as this?
The "normal" way to apply any sort of dynamic rendering to a Template based control such as the Repeater is to handle the ItemCreated or ItemDataBound events.
In your particular case, you could check appropriate conditions within that event handler and toggle the visibility of the relevant "Action" column.
Also, see this question where Ian Quigley posted a code snippet that should serve as a good example for you. It may also help to read my own answer which shows how to use visibility toggling in inline code.
This is what I'm doing: Using jquery, I'm popping up a "form" over the page that lets the user search for branches of the company and select the one they want. When the form pops up, they can type in a textbox, and it will do AJAX requests back to the server to return the top n results for what they've entered, and those results will be put into a list for them. I want the user to be able to select one by clicking a link that says "select" or something, and at that point I want it to do a PostBack have the Branch Selector control that this is in change it's SelectedBranch property to the newly selected branch. I've got this all working right now with a hard coded list of LinkButtons, but how do I do the same thing with a dynamic list of links inserted with jquery?
Look at the HTML that gets emitted for your hard coded LinkButtons. You'll see that each one calls the JavaScript __doPostBack function when clicked. I believe this function takes two arguments: a control ID and an extra command argument you can use for your own purposes.
I would suggest adding a single control to the page whose only job is handling events for the dynamic links. Then, when you are creating the links with jquery, make each one call __doPostBack, passing the event handling control's ID for the first argument and some other string for the second argument that identifies which link was clicked. In the Click event for the handling control, look at the second argument value and do what you need to do.
The short answer is... you don't.
ASP.NET relies on the Viewstate for the current state of the controls, including items in a DropDownList or similar control. Dynamically updating a list on the client will not modify the viewstate, so will not be available on the back end.
The general workaround for this is to just add a hidden field which updates/stores the current selection via js on the client side. Then read it from this field on the backend rather than List.SelectedValue.