Doing postback on a select with onchange without JSON - c#

How do I create a postback with a onchange while getting the value into a Request[]
The code:
<select name="nameOnSelection" id="idOnSelection" onchange="">
<option value="1">
random string
</option>
<option value="2">
random string
</option>
</select>
Im coding in cshtml. and im not looking for JSON solution.

Using jQuery you can easily find the closest <form> tag and submit it.
<select onchange="$(this).closest('form').submit();"/>

Related

ASP.NET MVC OnChange Razor

I have searched if there is any way that ASP.NET MVC can handle OnChange event without using neither Javascript but all the results return the OnChange of DropdownList...
Is there any way to achieve this? I want to OnChange an file input to post file data.
<select id="myElement">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
you can use javascript code
document.getElementById("myElement").addEventListener("change",function(e){
e.currentTarget.value
});
Without JavaScript, no, if you want to do anything dynamically, you need JavaScript.
As HTML is just for markup, and nothing else, you'll have to have the page refresh, or use JavaScript.

ASP.NET Core Tag Helper Arrays

I have an ASP.NET Core 2.0 web application that generates a multi-select list box using bootstrap-multiselect (basically a prettied up list box control)
The code generated is shown:
<select class="multiselect-ui form-control" id="Issues" multiple="multiple" name="Issues">
<option selected="selected" value="1">Dilapidated Structures</option>
<option selected="selected" value="3">Junk on Premises</option>
<option selected="selected" value="4">Junk Vehicles</option>
<option selected="selected" value="5">Minor Maintenance</option>
<option selected="selected" value="6">Miscellaneous</option>
<option selected="selected" value="7">Overgrowth</option>
<option selected="selected" value="8">Right of Way</option>
<option selected="selected" value="9">Sidewalks</option>
<option selected="selected" value="10">Trash</option>
<option selected="selected" value="2">Weeds/High Grass</option>
</select>
When this is submitted in a form, I see the Issues being added to the URL like so:
/Cases?Issues=1&Issues=3&Issues=4&Issues=5 ...
This is great and I am able to retrieve the values in my controller using a List<string> object. My question is, how would I add a hyperlink to the page that uses the values already sent via GET? Basically I would like to use Tag Helpers, but there is no way to add an array of values that I've found. There are dictionary key/pairs, but of course you are not able to add multiple values with the same key. I would like to be able to reference a List<string> in the tag helper itself (the following does not work, but should give an idea of what I'm after if I am sending the List<string> back using ViewBag):
<a asp-route-Issues=#ViewBag.IssuesList>Next</a>
I found out through this link that this feature isn't implemented. I used #Url.Action to build my link as shown (where ViewBag.IssuesList contains the List<string> -- I also left out some paramters for brevity:
Previous

C# - Webkit - Select a html dropdown item without selected attribute

I need help. How can i select a dropdown item that does not have the selected attribute in C# using Webkit? The html is :
<select id="question" name="questionId">
<option value="">Text</option>
<option value="1">
Question 1
</option>
<option value="2">
Question 2
</option>
</select>

Dropdown list options

I am using this code on my Master page and wants to display option name on the reffered page
<select name=menu onchange="location.href=(menu.options[menu.selectedIndex].value)">
<option value="http:google.com">Google</option>
<option value="http:myurl.com">URL</option>
</select>
You need to use this to refer to event source object instead of menu the name of html element. You also have wrong url form and need to change it from http:google.com to http://google.com
Live Demo
<select name=menu onchange="window.location.href=(this.options[this.selectedIndex].value)">
<option value="http://www.google.com">Google</option>
<option value="http://www.stackoverflow.com">stackoverflow</option>
</select>​

Using databinding to populate a HtmlSelect with optgroup elements in asp.net

I am using asp.net and have a HtmlSelect element on my page (with runat="server"). Normally I will just set the DataSource to some loaded data and DataBind. However in this case the data has one level of hierarchy and I want to represent this in the HTML with an optgroup. Google hasn't come up with any joy - is this even possible?
I've had a similar problem to solve and this is how I did it. Before I continue I did find all sorts of re-fracturing methods for lists, which probably do work. However, I did not think this the best route to go for my solution. Here's what I did:
I also needed to know which option a user selected in the drop down list but found that you can't build drop down list with option groups in ASP.net. So to do this I just built a simple <select> with <optgroup>'s and <option>'s in each group. Then I placed a <div> with a hidden textbox, which had the runat="server" set. I set the "onchange" event, on the select, to change the text value of the hidden textbox to whatever the value the option was, which was selected by the user, using javascript. Then, when the post back occurs, the code behind had access to the value the user selected, via the hidden textbox's runat="server". Problem solved! The code looks something like this:
Code in the HTML (aspx):
<div id="selectDiv">
<select id="selectItems" onchange="document.getElementById('hiddenselectDiv').getElementsByTagName('input')[0].value = this.options[this.selectedIndex].value;">
<optgroup label="Johannesburg">
<option value="Wilropark">Wilropark</option>
<option value="Bryanpark">Bryanpark</option>
<option value="Hurlingham">Hurlingham</option>
<option value="Midrand ">Midrand </option>
<option value="Glenvista">Glenvista</option>
<option value="Sunninghill">Sunninghill</option>
<option value="Edenvale ">Edenvale </option>
<option value="Parkhurst">Parkhurst</option>
</optgroup>
<optgroup label="Cape Town">
<option value="Tokai">Tokai</option>
<option value="Durbanville">Durbanville</option>
</optgroup>
<optgroup label="Durban">
<option value="Musgrave">Musgrave</option>
</optgroup>
<optgroup label="Pretoria">
<option value="Hatfield">Hatfield</option>
</optgroup>
</select>
</div>
<div id="hiddenSelectDiv">
<!--
Note: You probably want to set the hidden value to the first item you have seleced when you build the <select> object.
-->
<input type="hidden" id="selectedItem" value="Wilropark">
</div>
In the Code behind (C#):
if (!string.IsNullOrEmpty(selectedItem.Value))
{
//validation or whatever you want....
}
I've been looking around for the same thing, and it doesn't look like ASP supports this natively. One Google search found someone recommending a library at http://www.codeplex.com/SharpPieces, but I've never used it and I don't know how good it is. Other people talk about writing your own renderer for optgroup support.

Categories