Grails select not returning the object but a string

我是研究僧i 提交于 2020-01-04 10:59:09

问题


How can I get a select tag to return the object itself and not a string? Whenever I go to create a new BusinessArea I get an error that says:

"Failed to convert property value of type java.lang.String to required type bassscheduler.BusinessType for property businessType; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [bassscheduler.BusinessType] for property businessType: no matching editors or conversion strategy found"

Am I doing something wrong? I cant seem to find anything on how to get the select tag to return the object itself and not a string

Thanks for the help

I have the following model

class BusinessArea {

  BusinessType businessType
  // The date and time this occurence was created
  Date dateCreated
  // The date and time that this occurrence was last maintained.
  Date lastUpdated
  // The identifier for the OPERATOR or PROGRAM that last maintained this occurrence.
  int lastMaintainer
  // The name of the business area
  String name

  static constraints = {
    lastMaintainer blank: false, nullable: false
    name blank: false, nullable: false
  }
}

and controller action

def createBusinessArea() {

  def businessArea = new BusinessArea(params)
  if (!businessArea.save(flush: true)) {
    render view: "index", model : [businessArea: businessArea, activeTemplate: "businessArea"]
    return 
  }
  redirect controller: "admin", action: "index", model : [activeTemplate: "businessArea"]
}

With this form submitting to the controller action

<g:form controller="admin" action="createBusinessArea"> 
  <div class="row">
    <legend>Business Area</legend>
<div class="span3">
  <label>Business Area:</label>
  <g:textField name="businessArea"/>
</div>
<div class="span3">
  <label>Business Type:</label>
  <g:select name="businessType" from="${businessTypes}" optionValue="name"/>
</div>
  </div>

  <div class="buttons">
    <div class="row">
      <div class="span7" style="padding-top: 2em">
    <g:submitButton class="btn btn-primary pull-right" name="create" value="Create" />
  </div>
    </div>
  </div>
</g:form>

回答1:


This:

<g:select name="businessType" from="${businessTypes}" optionValue="name"/>

should be:

<g:select name="businessType.id" from="${businessTypes}" optionValue="name" optionKey="id" />


来源:https://stackoverflow.com/questions/13300313/grails-select-not-returning-the-object-but-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!