How can I create a dynamic image source with MVC3 and Razor? - asp.net-mvc-3

I have a view that attempts to build a table off of the passed in Model. I'd like to generate an image with a source that is changed based on the iteration of a loop but can't seem to get it to work. I can do this in JQuery but I would like to do it with razor. I have the following table body:
<tbody>
#{for (var ix = 0; ix <= Model.Value.Count - 1; ix++)
{
<tr>
<td style="width: 10%"><img src="http://gmap.com/marker" + #ix + 1 + ".png" /></td>
<td style="width: 75%">#Model("fullname")</td>
</tr>
}
<tbody>
That first TD is the problem area. I attempted it but... Thanks for the help.

Since it is razor you do not need to add the rendered output to your url. Try this:
#{for (var ix = 0; ix <= Model.Value.Count - 1; ix++)
{
<tr>
<td style="width: 10%"><img src="http://gmap.com/marker#(ix + 1).png" /></td>
<td style="width: 75%">#Model("fullname")</td>
</tr>
}
I haven't tested it but I believe it will fix your issue.

Related

Loop map with entity

Have some for:
for(int i = 0; i < ids.size(); i++){
List<NomenclatureTypeFirst> nmtf = nomenclatureTypeFirstService.getAllByRequest(ids.get(i));
map.put(ids.get(i), nmtf);
}
Tried to uset th:text on value like this:
<table>
<tr th:each="entry, stats : ${map}">
<td th:text="${entry.key}"></td>
<td th:text="${entry.value.detname}"></td>
</tr>
</table>
But catch:
Property or field 'detname' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?
How can I do this?
The value of your map is a list, so you have to iterate over the values:
<table>
<tr th:each="entry, stats : ${map}">
<td th:text="${entry.key}"></td>
<table>
<tr th:each="value : ${entry.value}">
<td th:text="${value.detname}"></td>
</tr></table>
</tr>
</table>

How to make table scrollable inside div with fixed table header of sementic ui?

I have make table scrollable inside div and everything is working fine but table header is too scrolling.I want to make table header fixed. And this table is rendered within a div. I have used PahingHelper model for paging.I have tried to use scrollable in table but it too is not working.So, am here to make scrollable-table. Is there any way to make table scrollable?
// here is my sample code for scrolling
#model ShresthaTrade.Helper.PagingHelper
<table >
<tr>
<th>Brand</th>
<th>Machine Type</th>
<th>Machine Model</th>
<th>Machine Serial</th>
<th>Mac Address</th>
</tr>
</table>
<div style=" overflow:auto; height: 340px;">
<table >
<tbody>
#if (Model.Datatable.Rows.Count > 0)
{
foreach (System.Data.DataRow row in Model.Datatable.Rows)
{
<tr>
<td>#row["brand_name"]</td>
<td>#row["machine_type_name"]</td>
<td>#row["MachineModel"]</td>
<td>#row["MachineSerial"]</td>
<td>#row["MacAddress"]</td>
</tr>
}
}
else
{
<tr>
<td colspan="4">No records are found</td>
</tr>
}
</tbody>
</table>
</div>

how to add list of objects to html table in spring MVC project?

I'm really new to Spring MVC and Hibernate and this is my first project with java ee technologies. I’m stuck at this point for almost 2 days and I searched over internet but no luck. What I only got is how to represent list of objects in html table fetched from database(which I'm not looking for).
I have a one to many relationship in my project with FeedOrderDetails and Feed. Everything works perfectly fine except I can’t save many feeds with feedOrderDetails. Case is I can save only defined number of feed items in the my jsp page.
Here is a picture of my ER diagram.
This is the code in my jsp
<table>
<tr>
<td>Date</td>
<td><form:input path="date" /></td>
</tr>
<tr>
<td>Discount</td>
<td><form:input path="discount" /></td>
</tr>
<tr>
<td>Total</td>
<td><form:input path="total" /></td>
</tr>
<tr>
<td>Unit Price</td>
<td><form:input path="feedOrderDetail[0].unitPrice" /></td>
</tr>
<tr>
<td>Quantity</td>
<td><form:input path="feedOrderDetail[0].quantity" /></td>
</tr>
<tr>
<td>Feed</td>
<td><form:input path="feedOrderDetail[0].feed.id" /></td>
</tr>
<tr>
<td><input type="submit" value="Add Feed"></td>
</tr>
</table>
</form:form>
What I want to achieve is I want to add many feeds to Feed Order and save it all together. Like I want to add feed Id, feed name ,quantity and unit price to html table and save all these details in one button click.
this is what I want to achieve.
I enter packing material ID,quantity and unit price and click add button to add those item to table. like that I want to add those item to a table in html form with path tag included in it.
for a example
first row should contain tag
<td><form:input path="feedOrderDetail[0].feed.id" /></td>
second row
`<td><form:input path="feedOrderDetail[1].feed.id" /></td>`
whenever I add items to table this should happen.
You should write some javascript to add form <input> elements with the names resembling to those produced with <form:input> tags and then submit them altogether. It involves a bit of js coding, once the form elements are properly set, springmvc will do the conversion into a parent+list of children. In this context, elements like <form:input path="feedOrderDetail[0].feed.id" /> will be redundant if there's one <form:input path="id"/>
You can see an example here.
I could solve this problem with javascript. hope it will help to someone else later.
<SCRIPT language="javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var counts = rowCount - 1;
var cell1 = row.insertCell(0);
var feed = document.createElement("input");
feed.type = "text";
feed.name = "feedOrderDetail[" + counts + "].feed.id";
cell1.appendChild(feed);
var cell2 = row.insertCell(1);
var unitPrice = document.createElement("input");
unitPrice.type = "text";
unitPrice.name = "feedOrderDetail[" + counts + "].unitPrice";
cell2.appendChild(unitPrice);
var cell3 = row.insertCell(2);
var quantity = document.createElement("input");
quantity.type = "text";
quantity.name = "feedOrderDetail[" + counts + "].quantity";
cell3.appendChild(quantity);
}
</SCRIPT>

Soritng Ascending and descending order after clicking on table header?

I am using Selenium WebDriver with Java.
I have a table where I have to click on header of each column and want to validate whether sorting functionality is working correctly or not. I need to check for both Asc and Desc order.
So I have to take the count of the table first and the get the header text so that I click on each col 1-1 and then need to validate the sorting.
How can achieve my expected result. Below is the HTML source code:
<div>
<table cellspacing="0" rules="all" border="1" id="ctl00_ContentPlaceHolder1_gvClinicalTrait" style="border-color:appworkspace;border-collapse:collapse;">
<tr class="gridTitleBar">
<th scope="col">Date Collected</th><th scope="col">Clinical Trait Data</th><th scope="col">Source</th><th scope="col">Value</th>
</tr><tr class="gridBody" align="left">
<td>11/6/2008</td><td style="width:200px;">A1C</td><td style="width:200px;">d</td><td>6.00</td>
</tr><tr class="gridBody" align="left">
<td>9/17/2008</td><td style="width:200px;">BP</td><td style="width:200px;">e)</td><td>104/54</td>
</tr><tr class="gridBody" align="left">
<td>7/12/2008</td><td style="width:200px;">BP</td><td style="width:200px;">g</td><td>124/56</td>
</tr><tr class="gridBody" align="left">
<td>6/21/2008</td><td style="width:200px;">BP</td><td style="width:200px;">t</td><td>110/72</td>
</tr><tr class="gridBody" align="left">
<td>6/14/2008</td><td style="width:200px;">BP</td><td style="width:200px;">n</td><td>120/70</td>
</tr>
</table>
</div>
How can achieve my expected result. Below is the HTML source code:
you need to have list or array with expected result.
You need to create loop like this for every test:
List<string> expectedValues = new List<string>("12","13","14");
int expectedNumber = expectedValues.Count;
int tdNumber = 2;
for (int i=1; i< expectedNumber; i++)
{
string result = driver.Find(by.xpath("//div[#id ='ctl00_ContentPlaceHolder1_gvClinicalTrait']/tr["+ i +"]/td["+tdNumber+"]")).Text;
if (result != expectedValues[i])
Assert.Fail("Wrong value!")
}
I didnt try this code, but even if its wrong it's will be pretty close to code that you need.

Dynamic table which contains multiple submit forms

I am developing a shopping cart website in MVC3 with razor. Below code is view code, in which I am showing multiple products, each product has quantity text box and one submit button. So, in controller how would I know which submit button is called by user and how to read text from Label?
<table>
<tr>
#foreach (System.Data.DataRow i in Model.dt.Rows )
{
using (#Html.BeginForm("addtocart", "Chocolatier",FormMethod.Post,null))
{
<tr><td> <img src=" #Url.Content("~/Content/abc.gif") " alt="Imge" height ="40" width ="40"/></td></tr>
foreach (System.Data.DataColumn j in Model.dt.Columns)
{
lbl = "lbl";
lbl = lbl+#cnt.ToString();
if(j.ToString().ToLower ().Equals ("name"))
{
<tr> <td style="width : 200px"><h5>#j.ToString()</h5><label id="#lbl" >#i[j].ToString()</label></td></tr>
}
if(j.ToString().ToLower ().Equals ("description"))
{
<tr> <td style="width : 200px"><h5>#j.ToString()</h5>#i[j].ToString())</td></tr>
}
if(j.ToString().ToLower ().Equals ("price"))
{
<tr> <td style="width : 200px"><h5>#j.ToString()</h5>#Html.LabelFor(m=>m.prod_price,i[j].ToString())</td></tr>
}
}
cnt += 1;
<td>Enter quantity : #Html.TextBoxFor(m=>m.prod_quantity)</td>
<tr><td><input type ="submit" value="Add to cart" id="#cnt"/></td></tr>
<tr> <td>__________________________________________________________________________</td></tr>
}
}
</tr>
</table>

Resources