I want the dropdown in the view to be get filled with value set from the controller. In my controller using model attribute I have added the list limits
#RequestMapping(value = "/works", method = RequestMethod.GET)
public String searchWorks(Model model){
Map< Integer, String > limits = new HashMap<Integer, String>();
limits.put(20, "20件");
limits.put(25, "25件");
limits.put(30, "30件");
limits.put(35, "35件");
limits.put(40, "40件");
limits.put(45, "45件");
limits.put(50, "50件");
model.addAttribute("limits",limits);
model.addAttribute("limit",limit);
return "workSituation";
}
In view Page in order to populate the dropdown with the value set using model attribute I have used this code
<div class="col-sm-3">
<select class="form-control" name = "limit" id="limit" onchange="getList(this.value)">
<option selected>-- select number of items--</option>
<option data-th-each="limit : ${limits}"
data-th-value="${limt.getKey()}"
data-th-text="${limt.getValue()}" >
</select>
</div>
I am getting the error
caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "limt.getKey()"
caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "limt.getValue()"
What will be the cause for this. Actually dropdown is iterating 7 times(the number of records in the limits list) but the key and value is not able to fetch with code.What is the correct way to get the dropdown filled with
<option value="20">20件</option>
<option value="25">25件</option>
<option value="30">30件</option>
<option value="35">35件</option>
<option value="40">40件</option>
<option value="45">45件</option>
<option value="50">50件</option>
For thymeleaf try the following
<option th:each="limit : ${limits.entrySet()}" th:value="${limit.key}" th:text="${limit.value}"></option>
Related
I have a problem with handling null value in select form and returning proper message, that this value can't be null.
My view (part of the form):
<label>Producer:</label>
<select class="form-control" th:field="*{producer.id}" th:errorclass="has-error">
<option value="0">Select producer</option>
<option th:each="producer : ${producers}"
th:value="${producer?.id}"
th:text="${producer?.producerName}">
</option>
</select>
<span th:if="${#fields.hasErrors('producer')}">
<ul>
<li th:each="err : ${#fields.errors('producer')}" th:text="${'- ' + err}"/>
</ul>
</span>
Part of Malt controller:
#NotNull
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="producer_id")
private Producer producer;
Spring (or thymeleaf?) requires value, so I set it to 0 - This relation is ManyToMany, and values are taken from database - there is no 0, so error is thrown:
org.thymeleaf.exceptions.TemplateProcessingException: Attribute "value" is required in "option" tags (template: "malt/createOrUpdateMaltForm" - line 55, col 9)
I have tried few different approaches, but I am not able to return any error message to the form, informing user to choose one of the available option.
How can I achieve this?
I have a dropdown that gets its items from database. The default value to be selected in this dropdown is retrieved from a cookie.
The problem is,
1. there is an empty option tag as the first option and also,
2. the default value is not selected.
There is no issue at all when I use v1.3.16. This happens when using v1.2.0. (I have to use only v1.2.0)
How to fix this.
ASPX:
<select id="ddlItems" data-ng-model="ItemId">
<option value="-1">-- All Circles --</option>
<option data-ng-repeat="item in Items" value="{{item.AreaCode}}"
data-ng-selected="item.AreaCode==ItemId">{{item.AreaName}}</option>
</select>
Ctrl.js:
var promise = MyFactory.GetItems();
promise.then(function (success) {
if (success.data != null && success.data != '') {
var data = success.data;
$scope.Items = data.lstItems; //bind items to dropdown
$scope.ItemId = itemIdFromCookie; //select default value
alert(itemIdFromCookie); //it pops -1
}
else {
console.log(success.data);
}
}
Rendered HTML:
<select id="ddlItems" data-ng-model="ItemId">
<option value="? string:"-1" ?"></option>
<option value="-1">-- All Circles --</option>
//...other options
</select>
Try to use ngOptions instead
Like this
<select id="ddlItems"
data-ng-model="ItemId"
ng-options="item.AreaCode as item.AreaName for item in Items">
<option value="-1">-- All Circles --</option>
</select>
This happens becausedata-ng-model="ItemId" is empty when your model is App is Loaded.
To Have some initial value. you can put default value in ng-init
For Ex : data-ng-init='data-ng-model="--SELECT--";'
Or per your Array list Item which you are getting from database set one of the values.
Remember init will get triggered b/f you complete the Ajax Call.
I'm trying to using thymeleaf as the view template and i come across one problem. the code I have now is as below.
<select>
<option th:each="res: ${result1}"
th:value="${result1.NAME}"
th:text="${result1.NAME}"></option>
</select>
What I need is like this, where I dont know how to add another attribute for result2, both result1 and result2 are the list object in the controller
<select>
<option th:each="res: ${result1}"
th:value="${res.NAME}"
th:text="${res.NAME+result2.SCHOOL}"></option>
</select>
You can create a simple object with two fields for result1 and result2, if you know their relationship in the controller. Then you can use a list of that objects to iterate in html page.
<select>
<option th:each="item: ${itemList}"
th:value="${item.result1.NAME}"
th:text="${item.result1.NAME + item.result2.SCHOOL}"></option>
</select>
I am having issues with modelmap attributes.. this is my xyz.jsp file..
<select name="list">
<option value="-">Choose a Value</option>
<c:forEach items="${sectionList}" var="section">
<option value="${section.code}">${section.description}</option>
</c:forEach>
</select>
and the controller class...
#RequestMapping(value="index", method = RequestMethod.GET)
public String mainList(ModelMap modelMap){
modelMap.addAttribute("sectionList", sectionService.getAllSectionList());
return "home";
}
But on the web page I don't see the options in the drop down.. All I see is only one value saying "${section.description}".. infact this is the source in html..
<select name="division" >
<option value="-">Choose a Value</option>
<option value="${section.code}">${section.description}</option>
</select>
Any help on what am I doing wrong here? Thanks!
did you try <c:out value="${section.[attribue]}/>? otherwise it will be displayed as string.
It looks like you dont have jstl-{version}.jar in your lib folder.
I have this code in my controller for Index view..
public ActionResult Index(int? id)
{
_viewModel.ServiceTypeListAll = new SelectList(_bvRepository.GetAllServiceTypes().ToList().OrderBy(n => n.ServiceTypeName).ToList(), "ServiceTypeId", "ServiceTypeName");
return View(_viewModel);
}
Using this I am able to dispaly all the ServiceTypes in my view in dropdownlist box. the code is
<%=Html.DropDownList("ServiceTypeListAll", new SelectList(Model.ServiceTypeListAll,"Value","Text"))%>
When I am trying to get the Selected Dropdownlist value from View to controller I am acceesing like this..
string categoryName = collection["ServiceTypeListAll"]; // collectoin refers FormCollection
I am expecting CategoryName should be string like what ever I am showing in Dropdownlist box.
I am getting Integer values?
is that somethign I am doing wrong
thanks
The value of a dropdownlist is its ID property, which you're specifying as ServiceTypeId.
You need to specify the ID as ServiceTypeName instead, like this:
_viewModel.ServiceTypeListAll = new SelectList(_bvRepository.GetAllServiceTypes().OrderBy(n => n.ServiceTypeName), "ServiceTypeName", "ServiceTypeName");
Also, Model.ServiceTypeListAll is already a SelectList; you don't need to wrap it:
<%=Html.DropDownList("ServiceTypeListAll", Model.ServiceTypeListAll)%>
This is what a select box looks like in HTML
<select>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
So the value you get in your controller is the selected value from the select box, and this is a number.
If you want to use the items text then there are two possibilities:
1) set the value of the options to the value of the text
public ActionResult Index(int? id)
{
_viewModel.ServiceTypeListAll = new SelectList(_bvRepository.GetAllServiceTypes().ToList().OrderBy(n => n.ServiceTypeName).ToList(), "ServiceTypeId", "ServiceTypeId");
return View(_viewModel);
}
2) after getting the int value load the appropriate object from your repository
int categoryId = Convert.ToInt32(collection["ServiceTypeListAll"]);
string categoryName = _bvRepository.Get(categoryId); // or whatever method loads your object
Your call to Html.DropDownList() will produce html that looks like this:
<select name="ServiceTypeListAll">
<option value="1">Service Type 1</option>
<option value="2">Service Type 2</option>
<option value="3">Service Type 3</option>
</select>
The value attribute is for whichever option is selected is what will appear in the FormCollection. If you really want the ServiceTypeName string instead of the ServiceTypeId, you can modify your SelectList constructor like this:
_viewModel.ServiceTypeListAll = new SelectList(_bvRepository.GetAllServiceTypes().ToList().OrderBy(n => n.ServiceTypeName).ToList(), "ServiceTypeName", "ServiceTypeName");
in order to produce html that looks like this:
<select name="ServiceTypeListAll">
<option value="Service Type 1">Service Type 1</option>
<option value="Service Type 2">Service Type 2</option>
<option value="Service Type 3">Service Type 3</option>
</select>
Also, incidentally, you should be able to simplify you HtmlHelper call to this:
<%=Html.DropDownList("ServiceTypeListAll", Model.ServiceTypeListAll)%>
No need to create another SelectList ...