MVC Razor Code Issue - asp.net-mvc

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>

Related

MVC View change specified row bgcolor

In my MVC view, I am displaying list of data from my model in a table as shown below. How to make a specified row to change color when one of the field satisfy certain condition within my if-else statement? It does not seems like I can add a jquery inside my if-else there for the purpose...
My View:
...
<table>
...
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.field1)
</td>
<td>
#Html.DisplayFor(modelItem => item.field2)
</td>
<td>
#if(item.field3== 0){
//change this row to grey color
}
else {
#Html.DisplayFor(modelItem => item.field3)
}
</td>
....
</table>
You just need to do it up where you are outputting the row. One way would be:
<table>
...
#foreach (var item in Model) {
<tr style='background-color: #(item.field3 == 0 ? "gray" : "white");'>
<td>
#Html.DisplayFor(modelItem => item.field1)
</td>
<td>
#Html.DisplayFor(modelItem => item.field2)
</td>
<td>
#Html.DisplayFor(modelItem => item.field3)
</td>
}
....
</table>
There are all sorts of ways to construct that conditional, just depends on your scenario. But the key is that you can access the field3 property of the item object anywhere in that foreach loop, just like a normal C# foreach

ASP.Net MVC model as dictionary in view

I would like to take the model as in view?
#model Dictionary<string,List<string>>
<table width="60%">
<tr class="ui-widget-header">
<td>
___Category___
</td>
<td>
___Detail___
</td>
</tr>
<tr>
<td>
#Model.Key
</td>
<td>
#Model.Count
</td>
</tr>
</table>
How could I do this?
here is my view with razor syntax.
Please help me.
Yes you can do it. Below is the sample
#model Dictionary<string,List<string>>
#foreach (KeyValuePair<string, List<string>> pair in Model)
{
<label> #pair.Key</label>
<div>
#foreach(string data in #pair.Value)
{
<span>#data </span>
}
</div>
}
Add #model Dictionary<string,List<string>> to your view (assuming Razor is used).
#model Dictionary<string, List<string>>
#foreach (var group in Model)
{
<h2>#group.Key</h2>
<table>
<tr>
<th>Title</th>
</tr>
#foreach (var aString in group.Value){
<tr>
<td>#aString </td>
</tr>
}
</table>
}

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)

Insert "where" rule in a "foreach" search

I need some help in order to put a where rule into the foreach search. My goal is to exclude orders where the customerOrder.ERPOrderNumber starts with letter E
The code that i have returns all the orders for the specific customer.
Thank you in advance for your help.
#foreach (var customerOrder in Model.CustomerOrders)
{
<tr>
<td class="mavo-order-date">#customerOrder.OrderDate.ToShortDateString()
</td>
<td class="mavo-status">#customerOrder.Status
</td>
<td class="mavo-order-number">
#customerOrder.OrderNumber
</td>
#if (Model.ShowErpOrderNumber)
{
<td class="mavo-erp-order">#customerOrder.ERPOrderNumber
</td>
}
<td class="mavo-po">#customerOrder.CustomerPO
</td>
<td class="mavo-order-total">#customerOrder.OrderGrandTotal.ToCurrency()
</td>
<td class="mavo-view">
<a class="btn btnStyleA tbm5" href="#Url.Action("OrderHistoryDetail", "Account", new { orderId = customerOrder.ERPOrderNumber })">View Details</a>
</td>
</tr>
}
You can put an if statement inside the foreach loop to only write out the table row when the order number doesn't start with the letter E.
#foreach (var customerOrder in Model.CustomerOrders)
{
#if(!customerOrder.ERPOrderNumber.StartsWith("E"))
{
// Markup goes in here
}
}
Or you can use LINQ to filter the CustomerOrders collection.
#foreach(var customerOrder in Model.CustomerOrders.Where(x => !x.StartsWith("E"))

Html.HiddenFor causing error

When I try to add #Html.HiddenFor(#Model.ID) to my code, I get the following error when access the page:
Compiler Error Message: CS0411: The type arguments for method 'System.Web.Mvc.Html.InputExtensions.HiddenFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I tried reading MSDN, but the documentation is awful (they don't provide a single code example in the documentation for this method.
Here is my View:
#model CustomerService.Entity.Order
#using CustomerService.Entity
#{
ViewBag.Title = "OrderDetails";
}
<h2>
OrderDetails</h2>
#using (Html.BeginForm("HandleSubmit", "Home", FormMethod.Post))
{
<table border="1">
<tr>
<td>
<b>Order #</b>
</td>
<td>
#Model.ID
</td>
</tr>
<tr>
<td>
<b>Description</b>
</td>
<td>
#Model.Description
</td>
</tr>
<tr>
<td>
<b>Salesperson Name</b>
</td>
<td>
#Model.SalespersonName
</td>
</tr>
</table>
<h3>
Line Items</h3>
<input id="btnAddLineItem" type="submit" name="AddLineItem" value="AddLineItem" />
#Html.HiddenFor(#Model.ID)
<table border="1">
<tr>
<td>
<b>Line Item ID</b>
</td>
<td>
<b>Description</b>
</td>
</tr>
#for (int i = 0; i < #Model.LineItems.Count; ++i)
{
<tr>
<td>
#Model.LineItems[i].ID
</td>
<td>
#Model.LineItems[i].Description
</td>
</tr>
}</table>
}
HiddenFor takes an expression.
#Html.HiddenFor( model => model.ID )
HiddenFor method should get an Expression as a parameter not a value:
#Html.HiddenFor(m => m.ID)
Instead of: #Html.HiddenFor(#Model.ID)
Method signature:
HiddenFor<TModel, TProperty>(HtmlHelper<TModel>,
Expression<Func<TModel, TProperty>>)
In plain text you should give an Expression that gets an "instance" of the type of your model(in this case CustomerService.Entity.Order) and returns the desired property(in this case ID)
MSDN

Resources