Smarty Display Array Values - foreach

I have the following array that I would like to display in Smarty:
->value = Array (2)
0 => Array (1)
0 => Array (4)
SiteName => "USA"
SanctionID => "41470"
Program => "Men"
Amount => "5.00"
1 => Array (1)
0 => Array (4)
SiteName => "USA"
SanctionID => "41471"
Program => "Men"
Amount => "5.00"
I am using two foreach loops, but am unable to get the values to display. appreciate any assistance.
{foreach from=$SXid item=Amount key=SiteName}
{foreach from=$SXid[sxid] item=Amount key=SiteName}
<tr>
<td></td>
<td>{$Amount} is {$SiteName}</td>
<td></td>
</tr>
{/foreach}
{/foreach}

try
{foreach from=$SXid item="outer" key="outer"}
{foreach from=$outer item="value" key="key"}
<tr>
<td></td>
<td>{$key|escape} => {$value|escape}</td>
<td></td>
</tr>
{/foreach}
{/foreach}

Related

Sum values from two columns in View MVC

I would like to sum values I get from two columns, these values are added from the data collected in View page based on customers so adding it in a controller from database is not an option as far as I know, I could be wrong
Any help would be appreciated.
I can get sum of two columns individually but can't get sum of two columns.
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.PkgBasePrice)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th>
#Html.DisplayNameFor(model => model.BasePrice)
</th>
</tr>
#foreach (var item in Model.Where(ModelItem =>
ModelItem.CustomerId.Equals(Session["CustomerId"])))
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.PkgBasePrice)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.BasePrice)
</td>
</tr>
}
</table>
<h2>
Package Total: $#Model.Where(c =>
c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.PkgBasePrice)
</h2>
<h2>
Product Total: $#Model.Where(c =>
c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.BasePrice)
</h2>
I would like to get sum of PkgBasePrice and BasePrice in one amount
Since you already have the two individual sums you want to combine, you can add the two sums you have together as follows:
#(#Model.Where(c => c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.PkgBasePrice) +
#Model.Where(c => c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.BasePrice))

Razor If-condition not working

I can't seem to get the if-condition of a String comparison to work. Here is the code for my foreach block.
#foreach (var item in Model){
<tr onclick="getData('#item.DiscountDescs');">
<td></td>
<td>#Html.DisplayFor(modelItem => item.DiscountDescs)</td>
<td>
#if (string.Equals(item.DiscountForm, "C"))
{
#:Card
}
else
{
#:Other
}
</td>
<td>#Html.DisplayFor(modelItem => item.DiscountType)</td>
<td>#Html.DisplayFor(modelItem => item.DiscountMode)</td>
<td>#Html.DisplayFor(modelItem => item.DiscountValue)</td>
<td>#Html.DisplayFor(modelItem => item.IsActive)</td>
<td>#Html.DisplayFor(modelItem => item.IsScheme)</td>
</tr>
}
The td display would always be "Other".
You need to trim the value coming from database.
Use Trim() function
item.DiscountForm.Trim()
This will omit extra spaces and will compare with string.

MVC Razor Code Issue

I'm trying to do something like the following:
<tbody class="searchable">
#foreach (var item in Model)
{
if (item.Account.AccountName != "xyz")
<tr class="danger">
else
<tr>
<td>
#Html.DisplayFor(modelItem => item.LocationName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
</tr>
}
</tbody>
The compiler doesn't like the if statement where I'm trying to create a row tag dynamically. The error I'm getting says the #foreach block is missing a close '}'. My guess is that the 'if' statement is being misinterpreted. Can someone suggest how I can fix this?
You can just avoid the if, and do something like:
<tr class='#(item.Account.AccountName != "xyz" ? "danger" : "")'>
<td>
#Html.DisplayFor(modelItem => item.LocationName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
</tr>

How to change Html.RadioButtonFor selection in MVC3?

My model has a list of 3 elements, each element has a string (AnswerBody) and a bool (correct).
When I submit the form, I get the values perfectly.
The problem is that, when selecting more than one radio button they all stay selected.
It shouldn't be like this. It should deselect the previous choice when selecting another one.
I'm one week stuck with this trick and I don't know how to solve.
Any help I'll be appreciated.
This is a part of my View:
<table>
<tr>
<td>
#Html.TextAreaFor(c => c.AnsLst[0].AnswerBody)
</td>
<td>
#Html.RadioButtonFor(c => c.AnsLst[0].Correct, true)
</td>
</tr>
<tr>
<td>
#Html.TextAreaFor(c => c.AnsLst[1].AnswerBody)
</td>
<td>
#Html.RadioButtonFor(c => c.AnsLst[1].Correct, true)
</td>
</tr>
<tr>
<td>
#Html.TextAreaFor(c => c.AnsLst[2].AnswerBody)
</td>
<td>
#Html.RadioButtonFor(c => c.AnsLst[2].Correct, true)
</td>
</tr>
</table>
If you are using a radio button group in which only one value can be selected at a time you should bind them to a single property on your view model which will hold the selected value and not to a collection:
<table>
<tr>
<td>
#Html.TextAreaFor(c => c.AnsLst[0].AnswerBody)
</td>
<td>
#Html.RadioButtonFor(c => c.Answer, "value 1")
</td>
</tr>
<tr>
<td>
#Html.TextAreaFor(c => c.AnsLst[1].AnswerBody)
</td>
<td>
#Html.RadioButtonFor(c => c.Answer, "value 2")
</td>
</tr>
<tr>
<td>
#Html.TextAreaFor(c => c.AnsLst[2].AnswerBody)
</td>
<td>
#Html.RadioButtonFor(c => c.Answer, "value 3")
</td>
</tr>
</table>
Now since the 3 radio buttons are bound to the same property on your view model (Answer) when the form is submitted this property will get the value of the selected answer. The value that will be sent is the one passed as second argument to the RadioButton helper.
So basically you will have the following property on your view model to store the answer:
public string Answer { get; set; }
In y example I have set some arbitrary values for the answers but you could use for example the id of the answer so that you could identify it:
#Html.RadioButtonFor(c => c.Answer, c.AnsLst[0].AnswerId)
#Html.RadioButtonFor(c => c.Answer, c.AnsLst[1].AnswerId)
#Html.RadioButtonFor(c => c.Answer, c.AnsLst[2].AnswerId)

MVCContrib, Html.Grid: How can I attach a row-based id to a td tag?

Here's my current view code:
<% Html.Grid((List<ColumnDefinition>)ViewData["Parameters"])
.Columns(column =>
{
column.For(c => c.ID);
column.For(c => c.Name);
}).Render();
%>
I'd like to attach an HTML "id" attribute to each "name" td tag as such:
<table class="grid">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr class="gridrow">
<td>1</td>
<td id="parameter_1">Address</td>
</tr>
<tr class="gridrow_alternate">
<td>2</td>
<td id="parameter_2">Phone Number</td>
</tr>
</tbody>
</table>
My question:
How do I do this?
I considered the 'Attributes' extension method, but I wasn't sure how I could make it work.
The syntax is rather bizarre, but it does work. Josh's answer is on the right track. Here's the full answer, using a line from my current project. This includes the syntax to use more than one attribute:
col.For(ts => ts.Subtitle.Abbreviation)
.Named("Subtitle<br />Language")
.Attributes(x => new Dictionary<string, object>()
{ // { "name", "value" }; results in:
{ "title", x.Item.Subtitle.Text }, // title="someLanguage"
{ "class", "someCssClass" }, // class="someCssClass"
{ "id", "someIdOrOther' } // id="someIdOrOther"
});
You can include as many name-value pairs as you want. Each will have access to the .Item property on the lambda variable (x in the above example) in case you need to use data from that row's object.
column.For(c => c.ID).Attributes(c=> new Dictionary<string, object>(){{"id", c.ID}});
Perhaps the best way to render the specified HTML would be to abandon the MVCContrib HTML.Grid entirely and just render the markup with a foreach.

Resources