0

这是我的代码:

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: step:1 -> 我需要使用复选框选择多个项目。步骤:2 -> 我必须获取tenant_id、tenant_location_id、tenant_page_id 和 item_id。

@controller:步骤:3 -> 我需要使用绑定从请求中获取值,我必须使用 item_id 找到项目名称、描述、价格并从 @model 获取。步骤:4 - >最后我必须存储选定的值(基于复选框中的长度)。

保存数据时,它只存储第一个迭代值,而不存储剩余的选定值。我不知道到底发生了什么,我还检查了数据正在接收,它正在节省一次

谢谢您的帮助。

4

1 回答 1

0

此代码可能对您有所提示:

Form<Tenantitem> tenantitemForm = form(Tenantitem.class).bindFromRequest();
Tenantitem tenantItem = tenantitemForm.get(); // bind as Tenantitem object

String[] itemid = request().body().asFormUrlEncoded().get("idd");
String[] postAction = request().body().asFormUrlEncoded().get("action");

if (postAction == null || postAction.length == 0) {
    ... // your code
} else {
    ... // your code
    if ("Add".equals(action)) {
        // declare new variable to store the data submitted
        Tenantitem tenantToBeSaved;

        for (String item: itemid) {
            // asign to an Item object?
            tenantToBeSaved = new Tenantitem();
            Item item_assigned = Tenantitem.findById(item)
            tenantToBeSaved.name = item_assigned.name;
            tenantToBeSaved.description = item_assigned.description;
            tenantToBeSaved.image_url = item_assigned.image_url;
            tenantToBeSaved.price = item_assigned.price;
            tenantToBeSaved.tenant_id = tenantItem.tenant_id;
            tenantToBeSaved.tenant_location_id = tenantItem.tenant_location_id;
            tenantToBeSaved.tenant_page_id = tenantItem.tenant_page_id;

            tenantToBeSaved.save();

            ... // rest of your code
        }
    }   
}
于 2013-05-14T11:27:20.083 回答