Razor If-condition not working - asp.net-mvc

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.

Related

dynamically generated table did not display

<table>
#if (Model.Logs != null && Model.Logs.Count > 0)
{
<tr>
<th>Operation Name</th>
<th>User</th>
<th>Parameters</th>
<th>Comment</th>
<th>Operation Time</th>
</tr>
foreach (var log in Model.Logs)
{
<tr>
<td>#Html.DisplayFor(model => log.OperationName)</td>
<td>#Html.DisplayFor(model => log.User)</td>
<td>#Html.DisplayFor(model => log.Parameters)</td>
<td>#Html.DisplayFor(model => log.Comment)</td>
<td>#Html.DisplayFor(model => log.OperationTime)</td>
</tr>
}
}
</table>
I'm trying to query some data from db and show them in a table via MVC. I set breakpoints and the codes seem to work well. But at last, the table didn't show on the page as expected. What's the possible reason? Thanks.
Are you sure it is looping when you put break point? If it does, try this. Make sure looping.
<table>
#if (Model.Logs != null && Model.Logs.Count > 0)
{
<tr>
<th>Operation Name</th>
<th>User</th>
<th>Parameters</th>
<th>Comment</th>
<th>Operation Time</th>
</tr>
foreach (var log in Model.Logs)
{
<tr>
<td>#log.OperationName</td>
<td>#log.User</td>
<td>#log.Parameters</td>
<td>#log.Comment</td>
<td>#log.OperationTime</td>
</tr>
}
}
</table>
Change the if-statement to a working statement where it returns something. I probably returns nothing at this moment.

Linq selecting some fields in MVC

I am trying to select some fields and send them to MVC View but it gives me this error
The model item passed into the dictionary is of type System.Collections.Generic.List'1[<>f__AnonymousType6'2[System.String,System.String]], but this dictionary requires a model item of type System.Collections.Generic.IEnumerable'1[IdentitySample.Models.ApplicationUser].
Here is my code:
public ActionResult Index()
{
var con = new ApplicationDbContext();
var allusers = (from u in con.Users
where u.Message != null
select new { UserName = u.UserName, Message = u.Message });
return View(allusers.ToList());
}
and in the View:
#model IEnumerable<IdentitySample.Models.ApplicationUser>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>#Html.DisplayNameFor(model => model.Message)</th>
<th>#Html.DisplayNameFor(model => model.Email)</th>
<th>#Html.DisplayNameFor(model => model.EmailConfirmed)
</th>
<th>#Html.DisplayNameFor(model => model.PasswordHash)
</th>
<th>#Html.DisplayNameFor(model => model.SecurityStamp)
</th>
<th>#Html.DisplayNameFor(model => model.PhoneNumber)
</th>
<th>#Html.DisplayNameFor(model => model.PhoneNumberConfirmed)
</th>
<th>#Html.DisplayNameFor(model => model.TwoFactorEnabled)
</th>
<th>#Html.DisplayNameFor(model => model.LockoutEndDateUtc)
</th>
<th>#Html.DisplayNameFor(model => model.LockoutEnabled)
</th>
<th>#Html.DisplayNameFor(model => model.AccessFailedCount)
</th>
<th>#Html.DisplayNameFor(model => model.UserName)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>#Html.DisplayFor(modelItem => item.Message)
</td>
<td>#Html.DisplayFor(modelItem => item.Email)
</td>
<td>#Html.DisplayFor(modelItem => item.EmailConfirmed)
</td>
<td>#Html.DisplayFor(modelItem => item.PasswordHash)
</td>
<td>#Html.DisplayFor(modelItem => item.SecurityStamp)
</td>
<td>#Html.DisplayFor(modelItem => item.PhoneNumber)
</td>
<td>#Html.DisplayFor(modelItem => item.PhoneNumberConfirmed)
</td>
<td>#Html.DisplayFor(modelItem => item.TwoFactorEnabled)
</td>
<td>#Html.DisplayFor(modelItem => item.LockoutEndDateUtc)
</td>
<td>#Html.DisplayFor(modelItem => item.LockoutEnabled)
</td>
<td>#Html.DisplayFor(modelItem => item.AccessFailedCount)
</td>
<td>#Html.DisplayFor(modelItem => item.UserName)
</td>
<td>#Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
#Html.ActionLink("Details", "Details", new { id=item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
Your view needs IEnumerable<IdentitySample.Models.ApplicationUser> but you just provide anonymous object by projection ApplicationUser in select new { UserName = u.UserName, Message = u.Message });
The best approach is using different views for different proposes. In each view choose appropriate #model and provide details for that model in your controller method.

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

Passing a simple value of a variable to a function wont work in my MVC asp.net

I have been wondering why I can't alert a simple string in my function. This is my code:
<script>
function getData(str) {
alert(str);
}
</script>
//This is the code in the body,
I just want to print a simple awe to test if the onClick can recognize the value I pass in name; but whenever onClick method happens, the web only alert an empty string.
#foreach (var item in Model) {
var name = "awe";
<tr>
<td></td>
<td>#Html.DisplayFor(modelItem => item.DiscountDescs)</td>
<td>#Html.DisplayFor(modelItem => item.DiscountForm)</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>
}
Change this:
<a href="#" onclick="getData(name);">
to:
<a href="#" onclick="getData('#name');">
name is nothing in this context, you have to put # for the c# variable.In view when we need to write c# code we write it with leading # sign:
For Example:
#foreach (var item in Model)
{
var name ="awe";
<tr>
<td>#name</td>
</tr>
}
If your intention is to include var name = "awe"; as a JavaScript variable, you need to put it in a <script> element:
<script>
var name = "awe";
</script>

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>

Resources