ModelMap attribute not passing the value - spring-mvc

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.

Related

How to populate dropdown in thymeleaf with hashmap set from controller

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>

asp.net mvc dropdownlist option disabled selected

Is there any elegant way to create something like this
<select>
<option value="" disabled selected>Select your option</option>
<option value="1">Option1</option>
</select>
using #Html.DropDownListFor helper in ASP.NET MVC 4?
http://jsfiddle.net/wLAt8/
Unfortunately not. There is no way to add attributes to a SelectListItem, which is what gets rendered as an <option>.
You would need to extend the SelectListItem class, and then extend the DropDownListFor to use it. It is unfortunately not very straightforward... It would have been nice for there to be an Attributes dictionary on SelectListItem for this purpose.
Here is an implementation of a custom attribute being applied:
Adding html class tag under <option> in Html.DropDownList
Here's one way you could do it using tag helpers.
<select asp-for="#Model.Orders.FruitsId" class="form-control">
<option value="">Select a Fruit</option>
#foreach (var Fruit in Model.Fruits )
{
if (barber.Name.Equals("Orange"))
{
<option disabled="disabled" value="#Fruit.Id">#Fruit.Name </option>
}
else
{
<option value="#Fruit.Id">#Fruit.Name </option>
}
}
</select>
After struggling I've done this via js function:
function handleSubAction(select) {
var item = document.getElementById(select);
item.children[0].disabled = true;
item.children[0].hidden= true;
return false;
}
and event handler in HTML:
<body onload="handleSubAction('subAction');"/>

How to add html document using crawler symfony2?

<select id="dealer_information_city" name="dealer_information[city]" required="required"><option value="" disabled="disabled">Choose a city.</option>
<option value="23">California</option>
<option value="114" selected="selected">Los Angeles</option>
</select>
Here's my code i want to add a new option using crawler symfony but nothing happen anyone can help me thanks!
var_dump($crawler->filter('#used_car_step1_car_model')->addHtmlDocument('<option value="1">New Jersey</option>'));

Spring MVC: Display correct value in select drop-down list

My JSP snippet is as follows:
<form:select path="rules[${counter.index}].assignedTo.assignedToName">
<form:options items="${assignmentRulesForm.assignedToList}"
itemLabel="assignedToName"
itemValue="assignedToName"/>
</form:select>
The assignedTo property refers to this object:
public class AssignmentDTO {
private String assignedToName;
// No other members
assignedToList then is a List<AssignmentDTO>
Really, what I want to happen is for the drop-down to contain all entries in the assignedToList, but to select the value associated with rule[i].assignedto.assignedToName
Presently, what I am seeing is that it does not perform the selection part, and the first item in the drop-down is displayed.
Any help is appreciated.
Thanks
This should work for you, the path is not the name but the assignedTo:
<form:select path="rules[${counter.index}].assignedTo">
<form:options items="${assignmentRulesForm.assignedToList}"
itemLabel="assignedToName"
itemValue="assignedToName"/>
</form:select>
If you have implemented a .equals for your assignedTo, it should just work.
<html>
<head>
<script>
function show() {
var op= window.document.getElementById('select');
var selItem= op.options[op.selectedIndex].value;
if(selItem=="Others") {
document.getElementById('text').style.visibility = 'visible';
}
else {
document.getElementById('text').style.visibility = 'hidden';
}
}
</script>
</head>
<select id="select" onchange="show();">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="Others">Others</option>
</select>
<br>
<input type="text" id="text" style="visibility:hidden">
</html>

how to get the string from dropdown list

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 ...

Resources