save variable from <select> in View into the ViewModel ASP-NET MVC - c#

So I have a ViewModel with this attribute
public string number { get; set; }
And in my View I have the next drop down list:
<select>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>
How do I save the value that gets selected on the drop down list into the number attribute from my ViewModel?
Usually I would use #Html.DropDownListFor(m => m.number, listObject) but the dropdownlist that I'm trying to create has to be dynamically created with JQuery, therefore I can't use the HtmlHelper.
EDIT: As answered in the comments, what I want to accomplish may be possible with the html helper but I wanted to avoid that.

Related

Using session variables in ASP Core 1.1

I want to fill a combo box located in the top menu; the menu is in the file _layout.cshtml and I don't want to fill from every action I call in my application.
There is a way to fill the combo using a session variable from the razor view?
Thanks
Your question is very broad and assumes a lot, Do you need to even store the values, can you just add a select list?
<select>
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
</select>

SelectValue on non-asp select list in ASP.NET C# (4.5)

I have the following html code (index.aspx):
<select class="ct-input ct-term" name="term">
<option value="5">5</option>
<option value="10">10</option>
</select>
I would like to select the proper item of the select list, regarding to querystring, from the code behind on the page load event. Is it possible? Querystring looks like: index.aspx?term=10 (10, so select the option with the value of 10).
I had the same issue with number input, but that one was easy, all I had to do was make it runat="server", then write name.Value = "something".
Unfortunately that won't work here, because my form doesn't have the attr runat="server", and I don't wanna add it, because then it would add a viewstate too and make the url unreadable. Is there any other solution? Ps.: form's method must be GET.
You could set the correct item into ViewBag in your action and in the view set selected based on the value in ViewBag. eg:
public ActionResult (int term){
ViewBag.Term=term;
return View();
}
<select class="ct-input ct-term" name="term">
<option value="5" <% if(ViewBag.Term==5){<text>selected</text>} %> >5</option>
<option value="10" <% if(ViewBag.Term==10){<text>selected</text>} %> >10</option>
</select>

Get value of Select Dropdownlist from code behind

I have a DropdownList which I have created using the Select Box as you can see below, now I am not sure how to get the value from code behind Is there anything I can use such as findcontrol in order to get the value. FYI: I am doing this in MVC using the aspx view engine not razor
Dropdownlist code:
<Select id="subjects">
<option value="Maths">Maths</option>
<option value="English">English</option>
<option value="Science">Science</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
</Select>
You should move data between View and Controller using Models. The model is sent to the view when the page is loaded, the model is post back again when you submit the form.

ASP MVC3 - Marking Option as Selected

I have a form that has 2 select boxes (Country and State) that have their available options contained in partial HTML files, due to their size.
In the event of a validation error, I want to mark the option the user originally selected as "selected".
Since the options are contained in a partial HTML file, I am not sure that this is even possible:
Any suggestions would be great. Thanks
// Inside the View
<select name="State" id="State">
#Html.Partial("States", Model);
</select>
#Html.ValidationMessageFor(model => model.State)
The partial simply contains the HTML:
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="AA">Armed Forces Americas</option>
<option value="AE">Armed Forces Europe, Middle East</option>
[...]
Instead of using strange Partial, why you just do not use helper DropDownList. You can find many examples, like this one http://peternewhook.com/2013/02/asp-net-mvc-selectlist-selectedvalue-dropdownlistfor/

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