這是我的代碼:保存數據時它僅存儲第一迭代值
Form<Tenantitem> tenantitemForm = form(Tenantitem.class).bindFromRequest();
String[] itemid = request().body().asFormUrlEncoded().get("idd");
String[] postAction = request().body().asFormUrlEncoded().get("action");
if (postAction == null || postAction.length == 0) {
return badRequest("You must provide a valid action");
} else {
String action = postAction[0];
if ("Add".equals(action)) {
for (String item: itemid){
tenantitemForm.get().name = Tenantitem.findById(item).name;
tenantitemForm.get().description = Tenantitem.findById(item).description;
tenantitemForm.get().image_url = Tenantitem.findById(item).image_url;
tenantitemForm.get().price = Tenantitem.findById(item).price;
tenantitemForm.get().tenant_id = tenantitemForm.get().tenant_id;
tenantitemForm.get().tenant_location_id = tenantitemForm.get().tenant_location_id;
tenantitemForm.get().tenant_page_id = tenantitemForm.get().tenant_page_id;
tenantitemForm.get().save();
System.out.println("name tenant_id"+tenantitemForm.get().name);
System.out.println("name tenant_location_id"+tenantitemForm.get().tenant_id);
System.out.println("name tenant_page_id"+tenantitemForm.get().tenant_page_id);
}
return redirect(
routes.Project.pts(loc_id,id)
);
} else if ("Cancel".equals(action)) {
System.out.println("Delete ID:"+id);
return ok("deleted");
} else {
return badRequest("This action is not allowed");
}
}
我views.scala:
@helper.form(routes.Project.addexist(loc_id,pageid,id,pageid)){
<div class="row-fluid" >
<div class="span3">
</div>
<div class="span9">
<div class="form-inline" id="options1">
<input type="button" class="btn" id="togglein" value="SelectAll" onclick="do_thisin()"/>
<label class="control-label offset1">
Filter By Name:
</label>
<input class="span3" type="text" placeholder="Enter name">
</div>
<ol id="selectable1">
<input type="checkbox" id="selectall" >
<input type="hidden" name="tenant_id" value="@id"/>
<input type="hidden" name="tenant_location_id" value="@loc_id"/>
<input type="hidden" name="tenant_page_id" value="@pageid"/>
@for(it <- item){
@if(it.tenant_location_id == locid){
<li>
<div class="well">
<div class="row-fluid">
<div class="span1">
<input name="idd" class="case" value="@it.id" type="checkbox">
</div>
<div class="span5">
<img class="img-polaroid span7" src="http://p.imgci.com/db/PICTURES/CMS/128400/128483.1.jpg">
</div>
<dl>
<dt>
<h4>Name:</h4>
</dt>
<dd>
<input type="hidden" name="name" value="@it.name"/>
<h5>@it.name</h5>
</dd>
<dt>
<h4>Price:</h4>
</dt>
<dd>
<input type="hidden" name="price" value="@it.price"/>
<h5>@it.price</h5>
<input type="hidden" name="description" value="@it.description"/>
</dd>
</dl>
</div>
</div>
</li>
<hr>
}
}
</ol>
</div>
</div>
<div class="row-fluid">
<div class="form-inline">
<div class="span5 offset6" >
<input type="submit" name="action" class="btn" value="Add"/>
<input type="submit" name="action" class="btn " value="Cancel">
</div>
</div>
</div>
}
我的功能性: @views: 步驟:1 - >我需要使用複選框選擇多個項目。 步驟:2 - >我必須得到tenant_id,tenant_location_id,tenant_page_id和item_id。
@controller: step:3 - >我需要從請求中使用綁定來獲取值,我必須使用item_id找到項目名稱,描述,價格並從@model獲取。 步驟:4 - >最後我必須存儲選定的值(根據複選框的長度)。
當保存數據時,它只存儲第一個迭代值,而不存儲剩餘的選定值。我不知道究竟發生了什麼,而且我也檢查了數據正在接收並且正在節省一次。
感謝您的幫助。
什麼是你想幹什麼,朋友?你可以解釋嗎?看起來這個聲明'表格 tenantitemForm = form(Tenantitem.class).bindFromRequest();'是無用的,因爲你想覆蓋你的迭代值。並且我總是不經常像'tenantitemForm.get()'和'Tenantitem.findById(item)''調用類似的東西,我建議將該值存儲在其他變量中。 –
2013-05-14 01:32:46
@Wayan Wiprayoga我需要根據我在VIEWS.html中的選擇保存多次。你有任何其他建議嗎?我也需要一些表單值來存儲到數據庫中,這就是爲什麼我使用綁定'.bindFromRequest();'。 – joan 2013-05-14 09:02:49
你可以發表你的觀點,以便我可以更加了解你想做什麼?所以,請通過解釋它來更新你的帖子,朋友。 – 2013-05-14 10:03:24