I am trying to display the values of this list of arrays, the problem is that the values are not displaying in the browser. I have the values of the list/arrays.
I don't know the problem or what's wrong with the syntax, any advice?
#{
List<string[]> validate_rules = new List<string[]>();
if (TempData["verification_errors"] != null)
{
validate_rules = (List<string[]>)TempData["verification_errors"];
}
}
#foreach (var item in validate_rules)
{
<label >#item[0].ToString() #item[2].ToString()</label>
}
You've surrounded the entire thing in a Razor Code Block. From the documentation:
Unlike expressions, C# code inside code blocks is not rendered.
I'm surprised it's even syntactically valid with the inline HTML inside a code block like that, unless there's an error that you're just ignoring. Remove the output from the code block. Something like this:
#{
List<string[]> validate_rules = new List<string[]>();
if (TempData["verification_errors"] != null)
{
validation_rules = (List<string[]>)TempData["verification_errors"];
}
}
#foreach (var item in validation_rules)
{
<p>#(item[0].ToString() + " " + item[2].ToString())</p>
}
The output can probably even be simplified to this:
#foreach (var item in validation_rules)
{
<p>#item[0] #item[2]</p>
}
Related
My question would be complicated but I will try to ask clearly.
I want to keep tags in a data at SQL, as using the comma in a cell. Then, try to call two different way on my MVC 4 project. One way (basic one) is working. But another one is hard to run. I will give my codes one by one.
SQL Table:
Controller:
public ActionResult Portfolio()
{
return View(db.Portfolios.ToList());
}
View:
#model IEnumerable<emirhanozkan.Models.Portfolio>
#{
foreach (var pot in Model)
{
<p>#pot.Tags.Split(',')[0]</p>
<p>#pot.Tags.Split(',')[1]</p>
<p>#pot.Tags.Split(',')[2]</p>
}
}
In same View one more foraech is running:
#foreach (var po in Model)
{
<li class="item #po.Tags.ToLower().Trim().Replace(",","")">
<img src="#po.Image" />
#po.Title
</li>
}
So, What I want to do with Split method:
<p>AngularJS</p>
<p>MVC</p>
<p>C#</p>
<p>Wordpress</p>
<p>MVC</p>
I guess my #pot.Tags.Split(',')[0], #pot.Tags.Split(',')[1] and #pot.Tags.Split(',')[2] code is wrong to list them but my brain not working anymore than that one. Please help me get them like my dream. Also, if you now to get just one from repeat words like <p>MVC</p> <p>MVC</p>, to just <p>MVC</p> please add to new code.
You can just loop on the array returned by Split() method and then render the tags:
foreach (var pot in Model)
{
var tags = #pot.Tags.Split(',');
foreach(var tag in tags)
{
<p>#tag</p>
}
}
sorry for errors, I write without compile:
List<Portfolio> Model = new List<Portfolio>();
StringBuilder finaltags= new StringBuilder();
foreach (var pot in Model)
{
finaltags.AppendLine("<p>" + #pot.Tags.Split(',') + "</p>");
}
#Html.Raw(finaltags.ToString());
foreach (var pot in Model)
{
var tags = #pot.Tags.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);
foreach(var tag in tags)
{
<p>#tag</p>
}
}
foreach (var pot in Model)
{
<li>
<img src="#po.Image" />
<a href="#po.Url" target="_blank" class="md-button md-primary">
#po.Title</a>
</li>
}
Thank you for your suggestions. But I found the answer to my question:
#{
foreach (var pot in Model)
{
string str = pot.Tags;
string[] strT = str.Split(',');
foreach (var poo in strT) {
<p>#poo</p>
}
}
}
I am trying to parse multiple page IDs into a cookie in Umbraco Razor.
I have this piece of code
#{
var siteroot = Model.AncestorOrSelf(1);
HttpCookie eventCookie = new HttpCookie("eventCookie");
}
#foreach (var child in siteroot.Descendants("Event").OrderBy("Date"))
{
if (DateTime.Today <= child.Date)
{
#RenderPage("~/macroscripts/RenderEventBox.cshtml", child, false)
}
}
#{
Response.Cookies.Add(eventCookie);
}
What I want to do is to create a CSV string in the foreach, with page IDs.
The page IDs should then be inserted in a cookie in which I can check for new IDs since the last time the user visited - for a "This is the new pages since your last visit" functionality.
But I am not sure how to do this.
The outcome that I want should look like
2525,4587,4789,4790,5858,5782,7899
which I then can put into a cookie.
Got it solved quite simple with
#{
var csv = "";
}
#foreach (var child in siteroot.Descendants("Event").OrderBy("Date"))
{
csv += "," + #child.Id;
}
I am working on an windows application and I am wondering what is the way to loop through an array of checkBox to see if they are checked or not and have a message box to show what is checked.
Here is the code i am using.
CheckBox[] myCheckBoxArray = new CheckBox[6];
myCheckBoxArray[0] = checkBoxALL;
myCheckBoxArray[1] = checkBoxA;
myCheckBoxArray[2] = checkBoxB;
myCheckBoxArray[3] = checkBoxC;
myCheckBoxArray[4] = checkBoxD;
myCheckBoxArray[5] = checkBoxE;
foreach(var items in myCheckBoxArray)
{
if(myCheckBoxArray.Checked)
{
MessageBox.Show(items);
}
}
You can use linq:
foreach(var checkedItem in myCheckBoxArray.Where(item => item.Checked))
{
MessageBox.Show(checkedItem);
}
You are looping through myCheckBoxArray, where items represent each item in the array, so you have to check whether the item is checked or not not the myCheckBoxArray.Checked. so your code will be like the following:
foreach(var items in myCheckBoxArray)
{
if(items.Checked)
{
//Do your stuff here
}
}
Dont loop not checked items .
var checkeeditems=myCheckBoxArray.where(p=>p.Cheked).toList();
foreach(var Name in checkeeditems)
{
MessageBox.Show(Name );
}
In the example code, you are looping through mycheckboxarray, but you are checking Checked property of myCheckBoxArray rather than item. You can alter the code as below and it will give you desired results.
foreach (var checkbox in myCheckBoxArray)
{
if (checkbox.Checked)
{
MessageBox.Show("this one is checked");
}
}
I am working on a combined EDIT / New item page and want to check a list for items and then call HTML.Action() on a default value if .Count > 0 or loop through the list calling the same HTML.Action() to return the same partial view with different data.
Code so far that is not working:
#{
var list = #Model.MyList.FindAll(x=>x.somval == someotherval);
if (list.Count == 0)
{
#Html.Action("MyFunc", "MyController", new { MyData = Default.Value });
}
else
{
foreach(var Item in #Model.MyList)
{
#Html.Action("MyFunc", "MyController", new { MyData = Item.Data });
}
}
}
The issue is that the closing } for the first #Html.Action() is causing a compile error (expected ','). There has to be something simple that i am missing but I cannot seem to find the issue through googling.
EDIT Added full code.
EDIT: Thanks to Ashley Medway answer (posted before I edited to include the full code) I now have the following that is working
#{var list = #Model.MyList.FindAll(x=>x.somval == someotherval); }
#if (list.Count == 0)
{
Html.Action("MyFunc", "MyController", new { MyData = Default.Value });
}
else
{
foreach(var Item in list)
{
Html.Action("MyFunc", "MyController", new { MyData = Item.Data });
}
}
Thanks for your help.
You need to use # before if statment and remove # before foreach.
#if (Model.MyList.Count == 0)
{
#Html.Action("MyFunc", "MyController", new { MyData = Default.Value })
}
else
{
foreach (var Item in Model.MyList)
{
#Html.Action("MyFunc", "MyController", new { MyData = Item.Data })
}
}
The code that I would expect to work would look like this:
#if (Model.MyList.Count == 0)
{
#Html.Action("MyFunc", "MyController", new { MyData = Default.Value })
}
else
{
foreach(var Item in Model.MyList)
{
#Html.Action("MyFunc", "MyController", new { MyData = Item.Data })
}
}
FIX 1
An if statement in a razor view needs to start with an #, assuming that the if statement is not wrapped an another code block.
Change if (Model.MyList.Count == 0) to #if (Model.MyList.Count == 0)
FIX 2
Remove # from inside the foreach loop. As we are already inside a code block we do not need the # to access the model.
Change foreach(var Item in #Model.MyList) to foreach(var Item in Model.MyList)
FIX 3
Removing ; from the end of #Html.Action(...). Semicolons are not needed to terminate a line in the razor view. When using HTML helpers, it would still be needed in a some scenarios but the compiler will let you know :)
Leaving the semicolon ; in will not actually break your view but it will result in semicolons being added to your rendered HTML.
I have a panel that is created and filled via a vacancy page I have created. Im doing it as follows:
#{
var root = CurrentPage.AncestorOrSelf(1);
var newsNode = root.Descendants("News").First();
var vacanciesNode = root.Descendants("Vacancies").First();
string shortenedSummary = string.Empty;
}
<ul>
#foreach (var vacancyItem in vacanciesNode.Descendants("Vacancy").Take(3).OrderBy("postDate desc"))
{
<p>here we are 2</p>
#vacanciesNode.Count().ToString()
<li>
<h4>#vacancyItem.jobTitle</h4> <span>Posted on #vacancyItem.postDate.ToString("dd/MM/yyyy")</span>
<p>
#if (vacancyItem.jobSummary.Length <= 182)
{
#vacancyItem.jobSummary
}
else
{
shortenedSummary = vacancyItem.jobSummary.Substring(0, 182) + "...";
#shortenedSummary
}
</p>
Read More..
</li>
}
</ul>
However, when there are no vacancy items, my list is empty. Should this be the case, I'm wanting it to read "sorry no vacancies just now" but I don't know how to check if my vacanciesNode has any items in it.
Could someone show me how I could achieve this?
Since the .Descendants() method returns a DynamicContentList (a collection) you can simply do a .Count() on the collection and check whether it's more than or equal to 1.
If there's more than 0 items in the collection, it's not empty.
So, what you need to do is surround your #foreach with an #if statement which checks on this, and an else statement after that prints whatever html you want to show if there's no vacancies
#if( vacanciesNode.Descendants("Vacancy").Take(3).OrderBy("postDate desc").Count() > 0) {
//Do foreach
}
else
{
//Write message about missing vacancies
}