ASP.NET MVC : render a string in a view - asp.net-mvc

I have this code part from a view
<td>
#if (item.ProductsRequest != null)
{
Html.TextBox("yes");
}
else
{
Html.TextBox("no");
}
</td>
But when I render it the string "yes" or "no" don't show up in the browser.
I want to write "yes" in the column if there is some information on the item.ProductsRequest.
Thank you for your help

If you just want to write the value out, then simply put the string in the respective if-else block via a <text> block:
<td>
#if (item.ProductsRequest != null)
{
<text>yes</text>
}
else
{
<text>no<text>
</td>
If you really want to be succinct:
<td>
#(item?.ProductsRequest != null ? "yes" : "no")
</td>

as Rion Williams just said, you can use this code below to accomplish your goal:
<td>
#if (item.ProductsRequest != null)
{
<text>yes</text>
}
else
{
<text>no<text>
</td>
But if you want to render using the TextBox function, you can do it this way:
#if (item.ProductsRequest != null)
{
#Html.TextBox("myTextBox", "yes")
}
else
{
#Html.TextBox("myTextBox", "no")
}

I want to write "yes" in the column
You don't need to use #Html.TextBox, just display text in this way
<td>
#if (item.ProductsRequest != null)
{
<span>yes</span>;
}
else
{
<span>no</span>;
}
</td>

Related

printing static message inside foreach loop

I am trying to print static message in the foreach loop section, but not getting any display message.
#foreach (var item in Model) {
<tr>
<td>
#*#Html.DisplayFor(modelItem => item.Status)*#
#if (item.Status == 1)
{
#Html.Display("active")
}
else if (item.Status == 2)
{
#Html.Display("inactive")
}
</td>
</tr>
}
Instead of printing the Status value, I am trying to print as above. But under the Status column I am not getting any message.
You can use #Html.Raw or special Razor tag <text>.
Here is a sample how to use them:
<td>
#*#Html.DisplayFor(modelItem => item.Status)*#
#if (item.Status == 1)
{
#Html.Raw("active")
}
else if (item.Status == 2)
{
<text>inactive</text>
}
</td>
I am assuming the Status is an integer
Do this instead:
#foreach (var item in Model) {
<tr>
<td>
#if (item.Status == 1)
{
<span>Active</span>
}
else
{
<span>InActive</span>
}
</td>
</tr>
}

MVC For..Each how to set class conditionally? [duplicate]

This question already has answers here:
Conditionally change CSS class in Razor view
(4 answers)
Closed 5 years ago.
I have a For...Each loop of a list in an MVC View. What I need to do is look at a property to determine if the records has been marked as deleted and then I would set the Bootstrap class of "danger" on a table row.
I can do a look at the property and write our the table row for each condition but that seems like extra work. If I use the following code Razor doesn't like the fact that it can't see the initial tag. Any suggestions on how to cleanly accomplish this?
#foreach (var item in Model.MyList)
{
if (item.IsDeleted == 1)
{
#Html.Raw("<tr class='danger'>")
}
else
{
#Html.Raw("<tr>")
}
<td>#item.FirstName</td>
<td>#item.LastName</td>
<td>#item.EmployeeId</td>
</tr>
}
Here is the simpler way for your problem
#foreach (var item in Model.MyList)
{
<tr class="#(item.IsDeleted ? "danger" : string.Empty) ">
<td>#item.FirstName</td>
<td>#item.LastName</td>
<td>#item.EmployeeId</td>
</tr>
}
How about something like this?
#foreach (var item in Model.MyList)
{
<tr class="#(Model.deleted ? "danger" : "")">
<td>#item.FirstName</td>
<td>#item.LastName</td>
<td>#item.EmployeeId</td>
</tr>
}
You can simply work with it this way
#foreach (var item in Model.MyList)
{
if (item.IsDeleted == 1)
{
<tr class="danger">
<td>#item.FirstName</td>
<td>#item.LastName</td>
<td>#item.EmployeeId</td>
</tr>
}
else
{
<tr>
<td>#item.FirstName</td>
<td>#item.LastName</td>
<td>#item.EmployeeId</td>
</tr>
}
or an alternative is
#foreach (var item in Model.MyList)
{
<tr #(item.IsDeleted != 1 ? String.Empty : "class=\"danger\"" )>
<td>#item.FirstName</td>
<td>#item.LastName</td>
<td>#item.EmployeeId</td>
</tr>
}

Writing conditional HTML with razor

Depending on my record, I'd like to change the style of the table row in my current iteration.
The below example doesn't work, but how would I go about doing this correctly?
foreach (var item in Model)
{
#{
if (item.DataState != DataState.Active)
{
<tr style="background-color: red">
}
else
<tr>
}
<td>#item.Name</td>
</tr>
}
So effectively, I'd like to dynamically render the <tr> element differently based on the DataState of my model.
Here's a shorter approach:
#foreach (var item in Model)
{
<tr #(item.DataState != DataState.Active ? "style=background-color:red" : "")>
<td>#item.Name</td>
</tr>
}
Edit: Code fixed
There are multiple way you can write condition.
Option 1:
#foreach (var item in Model)
{
if (item.DataState != DataState.Active)
{
<tr style="background-color: red">
<td>#item.Name</td>
</tr>
}
else
{
<tr>
<td>#item.Name</td>
</tr>
}
}
Option 2:
#foreach (var item in Model)
{
<tr style="#( item.DataState != DataState.Active ? "background-color: red;" : "" )">
<td>#item.Name</td>
</tr>
}
Define the attributes in a variable. The razor parser will omit the attribute if its value is null
#foreach (var item in Model)
{
var attributes = item.DataState == DataState.Active ? null : "background-color: red";
<tr style=#attributes>
<td>#item.Name</td>
</tr>
}
Not sure what kind of error you faces right now, but i gess your problem is that Razor don't understand all cases of tricky html render.
You can force him to render html where you need with #: symbol for single string or <text> tag like this:
#foreach (var item in Model)
{
if (item.DataState != DataState.Active)
{
#: <tr style="background-color: red">
}
else
{
<text><tr></text>
}
<td>#item.Name</td>
</tr>
}
You're probably going to get compile errors if you start splitting the <tr> tag up in the view and I'd not duplicate large chunks of code. I would do the following:
#foreach(var item in Model) {
string strStyle=String.Empty;
if(item.DataState!=DataState.Active) { strStyle = "background-color:red;" }
<text>
<tr style="#strStyle">
<td>#item.Name</td>
</tr>
</text>
}
#(item.DataState != DataState.Active ? "style=" + "background-color:red" : "")
#(!string.IsNullOrWhiteSpace(Model?.District?.Name) ? Html.Raw($"<span class=\"location\">{Model?.District?.Name}</span>") : string.Empty)

If/else in Razor View that checks html input value

In Razor, you can do something like:
#if (TempData["whatever"] != null)
{
blah blah
}
I want to check, in the if condition, the value of an input, for example:
<input id="hey" type="text" value="some text here"/>
#if ((#hey).value == "test")
{
blah blah
}
Obviously, the above doesn't work, it is just to show the intention. Is it possible?
In order to do that, you should use a Razor Helper class that create a textbox input and a model class:
Example
#Html.TextBoxFor(model => model.Name)
#if (Model.Name == "test")
{
<p>What a name.</p>
}
else
{
<p>Oh a name.</p>
}
But as devqon wrote, you should do it with JavaScript. Example :
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == null || x == "") {
alert("First name must be filled out");
return false;
}
}
Link: http://www.w3schools.com/js/js_form_validation.asp

MVC3 Razor - Formatting collection of strings

I have a collection of strings that are repeated in MVC3 Razor with the following code:
#if (Model.Publications != null)
{
<tr>
<th>Publications</th>
<td>
#foreach (var publication in #Model.Publications)
{
<text>#publication.Title</text>
}
</td>
</tr>
}
Now when I display this, all I get is:
Book1Book2Book3
But I really want is something like this:
Book1, Book2, Book3
Is there a simple way in MVC razor to achieve this without having to combine 'if' and 'foreach' statements?
#string.Join(",",Model.Publications.Select(p=>"<text>"+ p.Title+ "</text>"))
string.Join(", ", model.Publications.Select(pub => pub.Title).ToArray())
#if (Model.Publications != null)
{
<tr>
<th>Publications</th>
<td>
#var first = true
#foreach (var publication in #Model.Publications)
{
<text>#string.format("{0}{1}", first ? "" : ", ", publication.Title)</text>
#first = false;
}
</td>
</tr>
}

Resources