I have an MVC5/Bootstrap application that has a page where the user, as part of a larger form, can add and delete rows in a table.
To add rows, the user clicks an icon and then searches for data using a TypeAhead field that appears in a popover, which when selected retrieves row data using AJAX and inserts the row into the table.
Rows can be deleted by clicking on an icon in each row, the row is then removed. Both these actions affect a bound hidden input field that contains a list of IDs of the rows, e.g. "{100001},{100003},{100004}".
When the form that contains this table is submitted this hidden field is parsed and the appropriate database actions are performed using EF6 to add or delete items.
My question is whether this approach of using a single hidden field that is manually parsed by the controller method is the best choice. I was thinking there may be a more idiomatic way to utilize MVC model binding. e.g. having hidden checkboxes on each row or some such mechanism.
The ViewModel you are binding to could contain a collection of row viewmodels that wrap all inputs for a single row.
To submit these, bind them using the index.
<table>
<tbody>
<tr>
<td>
#Html.HiddenFor(m => m.RowData[0].SomeRowProperty)
<input type="hidden" name="RowData.Index" value="0" />
</td>
</tr>
<tr>
<td>
#Html.HiddenFor(m => m.RowData[1].SomeRowProperty)
<input type="hidden" name="RowData.Index" value="1" />
</td>
</tr>
</tbody>
</table>
See also Model binding to a list
Related
I have a GSP containing some rows with an checkbox which lets the user select several rows.
<table>
<thead>
<tr>
// Column headers
</tr>
</thead>
<tbody>
<g:each in="${itemList}" status="i"
var="instance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
// Some other rows
<td>
<g:checkBox name="selected"
value="${instance.id}"
checked="false" />
</td>
</tr>
</g:each>
</tbody>
And a pagination under my table:
<div class="pagination">
<g:paginate total="${total}" params="${params}"/>
</div>
Now the problem is when I switch from Page 1 -> Page 2 -> and then back to Page 1 all the checkboxes from Page 1 are reseted.
As pagination calls the list controller method I checked the following on page switch by Watching the following in Debugger when list controller method gets called:
params.list('selected')
But unfortunately the list is empty.
Pagination does not submit a form (or at least not like you're thinking it does; I really don't remember if the underlying logic uses a form or not) so your checkboxes are not being submitted to anywhere.
You need to consider different approaches for having your form submitted so that your checkbox selections are persisted. Two approaches could be:
Write your own pagination tag that wraps g.paginate and modifies which params are sent so that your checkbox values are included.
Add ajax handling to your checkboxes so that they are submitted immediately when changed.
I have a simple table and I have checkbox inside that table. If checkbox is selected I want to display other features.
My code:
<table>
<tr th:each="obj,iterationStatus : ${objs}">
<td th:text="${obj.name}"></td>
<td><input type="checkbox" name="firstcheckbox"></td>
<td th:if="isChecked">
<!-- do something -->
</td>
</tr>
</table>
How I do this without using javascript and backbean?
In this case you have two options, the first option you don't want but is not a bad option is using Javascript: creating a function called isChecked.
Otherwise, you can know if a checkbox is checked with thymeleaf in the case you are in a form which calls itself. You could do something like if(firstcheckbox != null) and, of course, your checkbox would have a value <input type="checkbox" name="firstcheckbox" value="value">. Finally, you must add as attribute in your model: model.addAttribute("firstCheckbox", firstCheckbox);
I want to create simple list in a form of table like on the image:
I am confused how to implement update/delete actions.
Both are [HttpPost] methods.
But I can't create forms inside <td> tags.
What is the valid way to make a table in the way that I want?
I think I have understood what do you need.
You can make one simple table and into each row put only one td which keep form.
Something like this:
<table>
#{
foreach (var item in Model.MyCollection)
{
<tr>
<td>
<form>
<table>
and hear put your existing row with your columns <tr><td>...</td><td>...</td></tr>
</table>
</form>
</td>
</tr>
}
}
and you will have one form for each record.
I have a Razor view rendering a nested collection which includes the following code...
<td>
#Html.TextBoxFor(x => x.Fitments[i].AncillaryProducts[ai].Description)
#Model.Fitments[i].AncillaryProducts[ai].Description
</td>
This should display an edit box followed by the text of the edit boxes value, but the resulting html is...
<td>
<input name="Fitments[0].AncillaryProducts[0].Description" class="valid" id="Fitments_0__AncillaryProducts_0__Description" aria-invalid="false" type="text" value="Valve (alloy/mobile)">
Valve (alloy/center)
</td>
Where the text boxes value "Valve (alloy/mobile)" is clearly different to the actual value "Valve (alloy/center)". How is this even possible?
The view is posted to the action method where AncillaryProducts is cleared...
foreach (var fitment in model.Fitments)
fitment.AncillaryProducts.Clear();
Then (different) ancillary products are added...
fitment.AncillaryProducts.Add(new JobCardAncillaryProduct() { Description = p.Description });
TextBoxFor is rendering the previous value of that array entry and Razor is rendering the current value.
Any help much appreciated.
I'll use the famous NerdDinner as an example here.
I have a search page where the user can enter a search string and then see the result in a table below. The user can also add more results to the table, like this:
Search for dinners today and display in a table.
Search for dinners tomorrow and add the result to the table.
The table will now show dinners today and tomorrow.
The user is also able to remove dinners from the table by clicking on them, one by one.
I need to generate a pdf with the results in the table. Not like a print screen because the pdf has it's own layout. I just need the data in the table. Preferably in a list of Dinner models.
Right now I can generate a pdf from a list of Dinner models. But once I've printed them to the table and the user has manipulated it I don't know how to get it back to a list of Dinner models.
Another solution could be to keep the Id's hidden in the table and then do another search in the DB with the Id's from the table (after the user has manipulated it). At least then I would get the result in the form of a list of Dinners. But this seems redundant to me.
Has anyone had a similar problem and how did you solve it?
You could put the table inside an html <form> and on each row in addition to the label you could have a hidden field:
#model IEnumerable<Dinner>
#using (Html.BeginForm())
{
<table>
<thead>
<tr>
<th>Prop1</th>
<th>Prop2</th>
<th>Prop3</th>
</tr>
</thead>
<tbody>
#Html.EditorForModel()
</tbody>
</table>
<input type="submit" value="Export to PDF" />
}
and in the editor template:
#model Dinner
<tr>
<td>
#Html.DisplayFor(x => x.Prop1)
#Html.HiddenFor(x => x.Prop1)
</td>
<td>
#Html.DisplayFor(x => x.Prop2)
#Html.HiddenFor(x => x.Prop2)
</td>
<td>
#Html.DisplayFor(x => x.Prop3)
#Html.HiddenFor(x => x.Prop3)
</td>
</tr>
Now this form could be submitted to the following controller action:
public ActionResult GeneratePdf(IEnumerable<Dinner> dinners)
{
byte[] pdf = ...
return File(pdf, "application/pdf", "dinners.pdf");
}
You may also checkout the following blog post for managing a variable length list in order to keep input field names in sync for the binder when adding/removing elements.