In my shopping store ASP.NET MVC application I am using a viewbag to store the url of a particular item (this url can be different for different items/products). So consider I have an item A having url A and item B having url B, if I open both items' pages simultaneously and let's say item B opens first and item A second, then the viewbag value of item A is overwritten by the value of item B.
Why is it so? Why is my viewbag overwritten?
Related
Say now for instance I have an HTML button <button id="1">button</button> in a view, how would I extract and store the id value of the button when it is pressed into a variable in my controller?
Some more context:
I have a list that generates dynamically (using a foreach loop) and each list item generates with a button that has a unique id as well so I am basically just trying to figure out which list item button was clicked using the id. I am trying to extract and store the id so that I can perform Edit and Delete operations on the corresponding list items.
The most effective way to pass a button id to the controller is to use a click event in javascript. For example.
<button id="1" onclick="buttonclick(this)">button</button>
In javascript side:
function buttonclick(e){
var id = e.id;
//use ajax to send data to the controller
}
How can i populate drop down lists in mvc5 such that each list is dependent on the previous drop down list?
Example:
Database stores information on cars first field drop down list should display all car manufacturers, depending on my selection the second field drop down list should only display data of car models for the brand i have selected.
You should use js for dropdown 'change' event. Then at least two options are possible.
Ajax request for next drop down values (different libs like select2 could handle that scenario out of the box) or form posting to the action, which will render it back with the next dropdown values provided.
I have got a radio button list and based on selection of the radio button list , the drop-downs will populate. Important thing is here the radiobutton list is set to autopostback=true.
And also when i move to next page by button click, And when i come back. Drop down button not able to maintain state. It is losing values. It is important for me to maintain state until i reach the last page. How can i approach this problem. I have used sessions but was not successful. Could you tell me how to implement sessions.
hi #Newyork167 this will not work as he mentioned above that "based on selection of the radio button list , the drop-downs will populate." so you have to store the "RadioButtonList" Selected value and accordingly fillDropDownlist Values and set selected after returning back to the page.
Ok. As per my understanding you need to save a page state so that when you come back then you get the page in previous state where you left. So to do this you have two ways.
1. Before jumping to next page, store everything in session like "Radiobutton selected value", dropdown selected value and other settings if any.
2. Pass these values "Radiobutton selected value", "dropdown selected value" and other if any as the query string and when you coming back then read the same query string.
In either way when you will come back then you will have the previous data. In the page_load event, just check whether you have that data or not. If yes then populate your controls with previous data else populate your controls for first load.
Here is some link for your reference.
http://www.codeproject.com/Articles/5876/Passing-variables-between-pages-using-QueryString
Passing Session[] and Request[] to Methods in C#
If you want to use sessions, you could check the session variable with all of the values of the list
if(Session["selectedList"] != Null){
var check = Session["selectedList"].ToString();
foreach(ListItem item in yourList.Items){
if(item.Value.Equals(check))
// set it as selected
}
}
For storing, when you click the button
Session["selectedList"] = yourList.SelectedValue;
You could also use the indexes instead of the values. You can also create a session variable for each dropdown/radiobuttonlist and you just make a loop for each.
UPDATE
Thanks to Shekhar for pointing this out. You need to go through all of the lists that you want to store and save them with these loops, not just the dropdowns. Then, you need to restore the radio button values, rebind the dropdowns, and then set the selected item for each.
When using javascript/ajax to repopulate the options in ComboBox B based on the selection in ComboBox A, selecting an item in B with an index > original set of indexes causes the error:
"'...' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value "
Evidently comboboxes still link back to their original datasource regardless of what javascript does, so there are issues with it thinking item 1 is x when it's really y, or item 2 doesnt exist. At least that's what I'm getting out of similar posts. What i'm not getting is a solution, tho...
If your ComboBox B was originally populated by databinding, your ViewState is going to reflect that, and your ComboBox B is going to be repopulated from the information in ViewState on postback. So your SelectedIndex of greater than the original number of items is going to confuse the heck out of it.
(The contents of ComboBoxes don't post back: just the selected item. So it's not going to know anything about what you did client-side.)
Possible options include: using an UpdatePanel to update your combo boxes without refreshing the whole page, or making your ComboBox B an ordinary HTML select, populating it exclusively from the client side (through Ajax?) and fetching its value from the Forms collection on the server side after postback.
I have a view where a user can enter a bank transaction. So they select an Account, a Payee, a Category (Based on allowable categories for the selected Payee) and a Sub Category (based on the Category selection) - and a value. This is done via JQuery, and is working.
However, there is a requirement to select more than one category/subcategory/amount per transaction (Split the transaction).
I'd like to maybe add a 'grid' (So, a table), and a button under the category/sub category/amount section, labelled 'Add', which then adds the selected Cat/SubCat/Amount to a grid, then blanks the existing selections, ready for more to be added - allowing the user to add as many 'amounts' as needed.
But - how?
On the Add button, I guess I would need to add the CategoryId, SubCategoryId and Amount to a List<>? Then based on that list, build the summary grid?
I'd like there to be no flashing of the screen. Would this have to be done via JQuery? Build the table with JQuery, and then have the list of cats/subcats/amounts sent back to the server on Form submittion?
You might want to look into the jQuery tmpl plugin. This plugins allows you to build client side templates. This is the kind of thing you might want to use on your site.
You define one template of a table row, and each time just add a new row based on that template.
I would give each form item in the table row an extra index variable (such as name="CategoryID-1" for the first row). While inserting a new row, you just send the current index to the template, and make sure the template adds the current index to the names of the form elements.
Then on the server side, you can do a simple query to get all the indeces.
ie
int[] indices = Request.Form.AllKeys.Where(k => k.StartsWith("CategoryID-")).Select(k => Convert.ToInt32(k.Substring(11))).ToArray();
foreach (int index in indices)
{
string cat = Request.Form["CategoryID-" + index.ToString()];
string subcat = Request.Form["SubCategoryID-" + index.ToString()];
//etc.
}