I do:
#Html.DropDownList("ddBrand", (IEnumerable<SelectListItem>)ViewBag.Brands, new {onchange = "JavaScript:Act()"})
But when I press submit it needs the field "ddBrand" in View Model.
How can I add DropDownList to View without binding to ViewData? (without adding field "ddBrand" in View Model)
Simply generate the HTML. You can create the options in a for loop. You can give different name to the id and name attributes.
<select id="ddBrand" name="ddBrand">
<option value="value">Text</option>
</select>
Related
I am doing a web application in Visual Studio in C#. The application should be able to write out students and courses which they attend. Anyway, I added a "Create view", where I can create new courses, but now I want to add a COMBOBOX there so that I am able to choose students and for the chosen one enter a new course. Can somebody tell me how to add a combobox WITHOUT using Windows Forms? I need code, and explanation where to insert it! I hope you can help me. Thank you.
You need to use <select> in html,to fill your list and dynamically create you need to fill your list in your controller like this:
List<Student> students = new List<Student>();
Student std = new Student();
....
students.Add(std);
Then return your View and pass it your list:
return View(students);
In your View set your View Model like this:
#model IEnumerable<Student>
Then add a foreach loop and create your comboBox:
<select id="studentList">
foreach(Student s in Model)
{
<option>#s.Name</option>
}
</select>
Or you can use Html.DropDownListForHelper method, and it will create a list automatically for you:
#Html.DropDownListFor(m => m.Name)
I give you a sample,you can change it like whatever you want,you didn't provide enough information...
Since I read that you did the "Create View" action, I assume you're making a web application in the ASP.NET MVC framework. A combobox on the web is merely this:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
-> Each "option" tag represents a choice in the dropdown.
Reference & more info on the "select" tag: http://www.w3schools.com/tags/tag_select.asp
But for full guidance on how to use the MVC framework I'd recommend you'd start a tutorial of some sorts, for example this one: http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started
The solution is not ASP.net specific, rather its part of html. The input control you're looking for is the select element. If you're using MVC, you can use the DropdownListFor extension method.
If you're using other server technologies they may have their own html rendering helpers.
I am using the regular html drop downlist (select) in a View.
I don't want to use Html.DropdownlistFor for some reason.
Now I am able to get the selected value in my Controller class.
How do I assign back a selected value back to the view?
<select name='ddlLocation' id='ddlLocation'>
<option value="">All </option>
<option selected="selected" value="1">City1</option>
<option value="2">City2</option>
<optgroup label="Group">
<option value="3">City2</option>
<option value="4">City3</option>
<option value="5">City4</option>
</optgroup>
</select>
Well there you go, you've already done it by using the selected attribute:
<option selected="selected" value="1">City1</option>
You should put the selected attribute on the value you want to be preselected.
I guess that now you realize your madness of not using the Html.DropDownListFor helper which automatically handles that for you. Because now you're gonna have to put #if else stataments turning your view into spaghetti code in order to achieve the desired behavior. But I guess you've got your reasons for not using the helper.
UPDATE:
From your comment it seems that your reason for not using the DropDownList helper is because it doesn't support optgroup. That's true and a good reason. But writing a custom helper is trivial and would really avoid you from spaghettifying your views with if/else statements for preselecting the default value.
There aren't many clean ways to go about this, but assuming you have some type of model bound to this page, you could set the selected value of the drop down based on the model value, as soon as the page loads :
<script type="text/javascript">
(function($) {
$(document).ready(function() {
var locationToSet = "#Model.LocationToSet";
$('#ddlLocation option[value="' + locationToSet + '"]').prop('selected', true);});
})(jQuery);
</script>
Im trying to change the code in an existing project to make a DropDownListFor to select an item. I have read countless of threads but i do not get an specific item to get selected;
This is what i got;
Controller;
ViewData("MyDropDownList") = new SelectList(_myRepository.GetData, "data_id", "name"})
View;
#Html.DropDownListFor(Function(m) m.data_id, TryCast(ViewData("MyDropDownList"), SelectList))
And this produces a nice list that looks something like this;
<select id="someId" name="someName" data-val="true">
<option value="aec385a7-bd77-4b94-9fbb-130487e3e62e">Option1</option>
<option value="5edee514-e6ca-456f-a8fa-71bde67351a1">Option2</option>
<option value="8a293328-8b11-47b7-bc9a-ceddf2e6a355">Option3</option>
</select>
After abit of reading i was pretty sure that this would work for making "Option2" selected;
ViewData("MyDropDownList") = new SelectList(_myRepository.GetData, "data_id", "name", "5edee514-e6ca-456f-a8fa-71bde67351a1"})
But it didn't, i have also tried this;
ViewData("MyDropDownList") = new SelectList(_myRepository.GetData, "data_id", "name", New With {Key .id = "5edee514-e6ca-456f-a8fa-71bde67351a1"} })
What am i doing wrong? VB is not my cup of tea so it might as well just be a syntax screw up. Any input is appreciated.
In MVC if the view is strongly typed the selectlist’s selected option will be overridden and the selected option property set on the constructor will never reach the view and the first option in the dropdown will be selected instead (why is still a bit of a mystery). To overcome this issue all that needs to be done is to rename the DropDownList in the view and also the name of the ViewData’s key in the controller to something other than the name set in the model , and the selected option will then reach the view. Unfortunately when posting the form you will have to access the selected option’s value from the FormCollection rather than a strongly typed object which is a little annoying.
Source :http://www.dotnetguy.co.uk/post/2009/06/25/net-mvc-selectlists-selected-value-does-not-get-set-in-the-view/
I'm trying to create a custom attribute for each list item in a databound HTML Select control.
The resulting HTML output should look something like this:
<select>
<option value="1" data-value="myValue1">item</option>
<option value="2" data-value="myValue2">item</option>
<option value="3" data-value="myValue3">item</option>
</select>
I've tried adding attributes like this, but they aren't being rendered:
<select id="selectList" class="multiselect" multiple="true" name="selectList[]" runat="server"></select>
ListItemCollection values = new ListItemCollection();
ListItem test = new ListItem("add");
test.Attributes.Add("data-value", "myValue");
values.Add(test);
this.selectList.DataSource = values;
this.selectList.DataBind();
Any thoughts on how this can be achieved? Thanks!
You have to add attributes to control's list items. Data-binding the list control may only set name & text. So simplest way is to add items manually instead of data-binding - for example:
ListItem test = new ListItem("text1", "value1");
test.Attributes.Add("data-value", "myValue1");
applicationList.Items.Add(test);
If you must use data-binding then you have to handle DataBound event and then iterate over the control's list items and add/set the required attribute. Frankly, I found this to be a round-about way of doing things.
I am building a screen in my C# Asp.Net MVC application, which has a few related drop downs.So, for example, I select 'Category', and then a 2nd drop down should display all related sub categories for the selected category.
So, would the model contain a List, used to build the Category drop down, and then a List, initially empty. On the selection of the category, I need to do a post back, passing back the id if the selected Category, then populate the List<> Sub Categories... and then build the Sub Category drop down?
Hopefully someone can assist with the design of my model. The model would also have quite a few non-list related data for the item being edited. And I think the model would also need a 'CategorySelectedId', so that I know what was selected?
The nicest way is to use AJAX for this. You'll have to hook the change event to your first select box, and once it is changed, you'll do a AJAX request with the selected value to some Action. The action will return a JSON list, which will be parsed and put in the next select box.
Update
The model for the JSON returning action can be an anonymous type really, or a IEnumerable of SelectListItem. Use Linq: `mycollection.select(item=> new SelectListItem() { Name = item.Name, Value = item.ID.ToString() });
If we assume the page looks like this:
<form>
<select id="MainCat" name="MainCatID">
<option value="1">First option</option>
<option value="2">First option</option>
<option value="3">First option</option>
</select>
<select id="SubCat" name="SubCatID" disabled="disabled">
<option value=""> -- Please select a main category -- </option>
</select>
</form>
Your model would look like:
public class MyModel {
public int MainCatID {get;set;}
public int SubCatID {get;set;}
public IEnumerable<SelectListItem> MainCats {get;set;}
}
Of course you could add validation attributes etc.