2012-11-09 104 views
2

如何獲取選擇標記以返回對象本身而不是字符串? 每當我去創建一個新的BusinessArea時,出現如下錯誤:Grails選擇不返回對象而是字符串

「無法將java.lang.String類型的屬性值轉換爲所需類型的屬性businessType的屬性bassscheduler.BusinessType;嵌套異常是java.lang。 IllegalStateException:無法將[java.lang.String]類型的值轉換爲所需類型[bassscheduler.BusinessType]的屬性businessType:未找到匹配的編輯器或轉換策略「

我做錯了什麼?我似乎無法找到如何讓選擇標籤返回對象本身,而不是一個字符串

感謝您的幫助任何

我有以下型號

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

和控制器動作

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"] 
} 

利用這種形式提交給控制器動作

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

回答

2

此:

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

應該是:

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