Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:19:07+00:00 2026-06-06T21:19:07+00:00

I want to create a <select> without using the helpers (because the select helper

  • 0

I want to create a <select> without using the helpers (because the select helper is generating a lot of html)

So, I get a list of cities from the Controller like:

public static List<City> getAllSortedByNameAsc() {
    List<City> cities = new ArrayList<City>();
    cities.addAll(City.find.orderBy("name").findList());
    return cities;
}

In my template, I create the options with this code:

@cities.map { city =>
    <option value="@city.id">@city.name</option>
}

which works, but I also want to have the chosen city as selected value. I tried several things like this:

@cities.map { city =>
    <option value="@city.id" selected="@if(offerForm("city.id").value == city.id){selected}">@city.name</option>
}

But that doesn’t work. Can anyone give me a hint?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:19:08+00:00Added an answer on June 6, 2026 at 9:19 pm

    Configuration File: application.conf

    db.default.driver=org.h2.Driver
    db.default.url="jdbc:h2:mem:play"
    ebean.default="models.*"
    

    Controller Class => Application.java

    package controllers;
    
    import play.*;
    import play.mvc.*;
    import models.City;
    import play.data.Form;
    import views.html.*;
    
    public class Application extends Controller {
    
      final static Form<City> cityForm = form(City.class);
    
      public static Result index() {
        City pune=new City();
        pune.name="pune";
        pune.save();
    
        City mumbai=new City();
        mumbai.name="mumbai";
        mumbai.save();
    
        City city=City.get(2L);
    
        return ok(index.render(cityForm.fill(city),City.all()));
      }
    
    }
    

    Model Class => City.java:

    package models;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import play.db.ebean.Model;
    import java.util.List;
    import com.avaje.ebean.validation.NotNull;
    
    @Entity
    public class City extends Model{
    
        @Id
        public Long id;
    
        @NotNull
        public String name;
    
        public static Finder<Long, City> find = new Finder(Long.class, City.class);
    
        public static City get(Long id){
            return find.byId(id);
        }
    
        public static List<City> all() {
            return find.all();
        }
    
    }
    

    Template File => index.scala.html

    @(cityForm: Form[City],cities: List[City])
    
    <!DOCTYPE html>
    <html>
        <head><title></title></head>
    
        <body>
            <div>
                <select>
                    @for(city <- cities){
                        <option value="@city.id" @{if(city.id.toString().equals(cityForm("id").value)) "selected='selected'"}/>@city.name</option>
                    }
                </select>
            </div>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to select a same database? I want to create a table from already
I am trying to populate a select list with time. I want to create
I am using Zend Framework. I want to fetch record from database without considering
I want to create the following T-SQL statement: SELECT SUM (sa.Amount) as 'SumAmount', SUM(sa.Cost)
I want to create JSF table which has select all button and check box
I want to write a stored procedure like this Create Proc dbo.GetApplicantsByIDs as Select
I created a customized CursorAdapter and want to select a list item, in order
This is killing me! I'm using django-filebrowser, and I want to create a gallery
I want to create a list out of an xml file where only values
How to create an html look up field. So, I want to achieve an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.