-1

On my program I have a Product structure

type Product struct{
    SupplierId          string
    Category1           string
    Category2           string  
    DefaultColor        string 
    SupplierRef         string  
}

On the form I have

    <div class="form-group row">
          <label for="category2" class="col-sm-2 col-form-label">Sub Category</label>
          <div class="col-sm-4">
            <select class="form-control" value="{{ .Category2 }}"  name="category2" id="category2">
              <option value="Select">Select</option>
              <option value="1">Dresses</option>
              <option value="2">Skirt</option> 
              <option value="3">Blouses</option> 
              <option value="4">Pants</option> 
              <option value="5">Hand Bags</option> 
            </select>
          </div>
        </div>

To move date from data (from source to form) I have the following code

 product := Product{}
    product.Category2 = "5"     //for Hand Bags

Problem : My form does not render with the selected item. (I am new to Go/html)

johannchopin
  • 13,720
  • 10
  • 55
  • 101
bheki
  • 21
  • 3
    Where's the Go code that renders the form? Can you please edit your question to provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – Adrian Nov 01 '19 at 13:51

1 Answers1

3

You have to use the "selected" attribute of the option:

<option value="1" {{if eq .Category2 "1"}}selected{{end}}>Dresses</option>
<option value="2" {{if eq .Category2 "2"}}selected{{end}}>Skirt</option>
...
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59