Problem With Struts Select Default Value - struts2

I am using a LinkedList of a very simple custom class I created to fill the select statement. I got everything set properly (values and keys), but I can not load my default value.
This is the code I used for my select statement:
<s:select name="reports" list="filters" listKey="id" listValue="name" value="%{filterName}" onchange="filterResults(this.options[this.selectedIndex].text,this.options[this.selectedIndex].value)"/>
The filters list that is used has an id field (Integer) and a name field (String).
I know that the value in filterName should match with a value in the drop down list. I printed it to the screen with a property tag, but nothing is selected when the page loads. Any help is much obliged.

Related

drop down list for using mvc 5

I was looking for a dropdownlistforcexample and found this is what I need There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CategoryID'
but I can't understand the "-- Select --" in the view page, if it means the select statement, well it's already mentioned in the controller, do I have to mention it again?
Its the option label. The label that is shown when the page got rendered and no option is selected.
optionLabelType: System.String
The text for a default empty item. This parameter can be null.
See: enter link description here

Kendo-UI DropDownListFor - selected index

I need to change standard dropdown list to the one from kendo and and I've got a problem with getting appropriate selected value.
This code gives me a dropdown list with correct values, but doesn't give selected value (first one is selected).
#(Html.Kendo().DropDownListFor(m => m.CountryName)
.BindTo(Model.AllCountries)
.OptionLabel("-- Select country --")
)
Inside span (generated by kendo) with this dropdown I found input (type = text) with all values as options, and this input has correct value.
How to display this value in my kendo dropdown?
Thanks
Have you tried setting the name of the DropDownList equal to the name of the property in your model? The Kendo Documentation suggests that "if widget's name is different than the Model's property then the ModelBinder will not be able to update the model."

SelectList method MVC

Problem Description:
I am using ASP.NET MVC and I have the following method in my Controller Class. This method uses SelectList method to select a list of items from the database. These items will then be passed to the view to be displayed in a drop down list.
public ActionResult Edit(int id)
{
Album album = db.Albums.Find(id);
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
}
My Understanding of the selectMethod:
I know that the first param takes a list of items.
The third param is the property to be displayed.In this case, we will be displaying the name property of the Artists.
Based on my research, I found out that the 4th param is the default value that will be displayed on the dropdown list.
My Question:
1) I would like someone to help me understand about the second param.
2)Since we are displaying names of the artists, how can we display a default artist name in the fourth param using Artist ID?
I hope you guys understood my questions. I would be happy to clarify them to you if you need me to do so.
The second parameter is the name of the "value" property or field on each element in the first parameter. Since two artists could conceivably have the same, it's generally better to use an ID column (as in the example) so that you know specifically which item was selected.
In the example, you are providing the artist of the currently-viewed album as the default to appear. Assuming that db.Artists includes an artist whose ArtistId property matches the given value, the rendered HTML will produce a select list where that artist is selected.
Did that answer your questions?
Per the documentation:
The first parameter is an IEnumerable of objects from which to construct the list.
The second parameter is the name of the object property (for each object in the list) to be used as the value attribute of each rendered HTML <option> element.
The third parameter is the name of the object property (for each object in the list) to be used as the text attribute of each rendered HTML <option>.
The fourth parameter is the default selected value (which indicates the element of the list that will be rendered with the selected attribute).

GSP- Select tag. How to achive selected="selected"

I have a select tag in my GSP file as
<g:select name="clientId" id="clientId" size = "4" from="${com.springcommunity.fleet.partymodel.roles.ClientRole.list()}" class = "filter_combo" optionKey="id" />
i want client with id 2 is selected initially (in simple html it is achived by using selected="selected")
how can i do it?
You need to specify the value attribute in this tag. http://grails.org/doc/2.0.x/ref/Tags/select.html
So in your example,
<g:select ... value="${com.springcommunity.fleet.partymodel.roles.ClientRole.get(2)}" />
One thing to be aware of here is that the value that you're selecting must be an object equal to the item in the list, and not just an id - this is where a lot of people get tripped up. So you can't just say value='2', you need to specify the object in the list that you have in your from attribute.
From the docs -
value (optional) - The current selected value that evaluates equals()
to true for one of the elements in the from list.

String[] vs Integer[] in Struts2 action attribute

I am new t struts2. i built one page, that shows list of employees. I can seach the employes based on his name by applying filter criteria and click on find button. At the same time, i provided check box in the left side for each employee name for delete operation. for all checkboxes, i gave Integer[] attribute name which is declared in Custom Actionclass.deleteaction is working fine. But when i click Find button the action is not getting submitted. Then i changed Integer[] to String[] both functions are working fine. What will be the problem? is it something like, attributes should only be String type.
The cause of your problem is that the Struts2 checkbox sets a boolean property on the action class:
Struts 2 Form Tags: Checkbox
When you defined the checkboxes as an Integers, the framework couldn't covert the boolean to an Integer. However it was able to convert the boolean to Strings. If you check the results in your action class, you should see the String[] populated with "true" and "false".
In general Struts2 is pretty good at converting submitted form data to whatever object type you want. Check out the docs on type conversion for more info.

Resources