Trying to use an index in a razor for loop - asp.net-mvc

I have the following code:
#{
var index=0;
}
#foreach (AdminSummary adminSummary in #Model.AdminSummaries) {
index++;
<div class="rep_tr0">
<div class="rep_td0">#adminSummary.RowKey</div>
<div class="rep_td0">Edit</div>
<div class="rep_td0">Delete</div>
<div class="rep_td0">#Html.TextBox("position_" index, #adminSummary.Position, new { size = 5 })</div>
<div class="rep_td0">#adminSummary.Title</div>
<div class="rep_td0">#adminSummary.DetailCount</div>
<div class="rep_td0">#adminSummary.Modified</div>
<div class="rep_td0">#adminSummary.ModifiedBy</div>
</div>
}
I am trying to find a good way to add an index value to the position name.
But this keeps giving me the error: CS1026: ) expected for the line with position_ on
Does anyone have any idea what I might be doing wrong?
Update:
I tried the following:
<div class="rep_td0">#Html.TextBox("position_"#(index), #adminSummary.Position, new { size = 5 })</div>
This gave the error: CS1646: Keyword, identifier, or string expected after verbatim specifier: #

Try
<div class="rep_td0">#Html.TextBox(string.Format("position_{0}", index), #adminSummary.Position, new { size = 5 })</div>

<div class="rep_td0">#Html.TextBox("position_"+ index.ToString(), #adminSummary.Position, new { size = 5 })</div>
just replace this line with above.

Try wrapping your inserted razor in ()
Example
Also, sometimes if you are in a razor block, the compiler requires you to wrap html if it is the only thing on the line,like div tags, in
<text><div class="rep_tr0"></text>
EDIT: Sorry, misread the question a little, after index++
string textBoxId = "position_" + index;
THEN:
<div class="rep_td0">#Html.TextBox(#textBoxId, #adminSummary.Position, new { size = 5 })</div>

Nice, but instead #foreach(), try use #for(), then your loop has its own index.

Related

some reason Cannot read property addEventListener of null

I got this error. I tried to dinamicly add some html tags by JS.. And there is a ID name is well what I need for addEventListener. But I dont know how to fix it.. If I change '#answer-' + i to .answers is working.. But this is not solution because I could click for any button without get the clicked ID..
Questions.prototype.displayQuestion = function() {
let answer = [];
questionDOM.innerHTML = this.question;
let correct = this.correct;
for(let i = 0; i < this.answer.length; i++){
answer.push(
`
<div id="answer-${i}" class="answer">
<h5>${this.answer[i]}</h6>
<div class="check"></div>
</div>
`
);
document.querySelector('#answer-' + i).addEventListener('click', function() {
if(i === correct) {
document.querySelector('#answer-' + correct).classList.add('animate__animated', 'animate__heartBeat');
} else if (i !== correct) {
document.querySelector('#answer-1').classList.add('animate__animated','animate__headShake')
}
})
}
answerDOM.innerHTML = answer.join('');
}
index.html
<body>
<div class="container">
<div class="question">
<div class="questionbox animate__animated animate__bounce">
<h2 id="question"></h2>
</div>
</div>
<div class="answers"></div>
</div>
<script src="app.js"></script>
You are pushing to your answers array but not actually adding the nodes to the dom. You want to do something like document.body.innerHTML += answer[i] before you try to get the element by id. I can also see that your loop won't execute because the length of answer is initially zero before you add to it within the loop. You need to add to answer before the loop or change the loop condition to <= this.answer.length <--- but that is an infinite loop

Umbraco 8 search function

I'm trying to implement a basic search functionality on Umbraco 8. So I created a search page, and here is my template:
#inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.Search>
#using ContentModels = Umbraco.Web.PublishedModels;
#{
Layout = "master.cshtml";
}
<div class="container">
#{
var searchQuery = Request.QueryString["query"];
if (!string.IsNullOrEmpty(searchQuery))
{
<div class="searchresults">
<p>Your search results for <strong>#searchQuery</strong></p>
<ul>
#foreach (var result in Umbraco.Search(searchQuery))
{
<li>
#result.Name
</li>
}
</ul>
</div>
}else{
<h1>No results</h1>
}
}
</div>
But I got a compilation error when trying to use Umbraco.Search() saying:
"UmbracoHelper" does not contain a definition for 'Search'.
Any ideas?
Thanks
UPDATE:
You need to use Umbraco.ContentQuery.Search instead of Umbraco.Search
Instead of Umbraco.Search, try using Umbraco.ContentQuery.Search.
It will work
refer link,
https://our.umbraco.com/forum/using-umbraco-and-getting-started/96691-umbraco-search-function

Multy Media picker, razor syntax newbie

i have used stackoverflow for a long time to do research bit this time i have a question of my own.
I am using umbraco and the razor syntax for the first time.
The idea is that i have a Repeatable text string property and a Multi Media picker and i am trying to display them in 3 columns with the first item from each multiple and repeatable in the 1st column etc...
Here is what i have so far:
<!-- MAIN SECTION -->
<section class="main">
<div class="wrap row small-up-1 medium-up-3 large-up-3">
#{
var something = new object();
var dynamicMediaItem= Umbraco.Media(something);
}
#if (CurrentPage.HasValue("icons"))
{
foreach (var icon in CurrentPage.icons.Split(','))
{
dynamicMediaItem = Umbraco.Media(icon);
}
}
#if (CurrentPage.repeatableText.Length > 0)
{
foreach (var line in CurrentPage.repeatableText)
{
<div class="columns column-block">
<img src="#dynamicMediaItem.umbracoFile" />
<h2>About Me</h2>
<p>#line</p>
Learn more
</div>
}
}
</div>
</section>

C# foreach within foreach, display 3 item in each item. carousel

I am using asp.net MVC 4 for my project, and I have a Model.
and I am using Bootstrap Carousel to show products for my Index Page (see the image below)
Now my question is: I want to show 3 Products in each slide item.
should I write a ViewModel for this?
<div class="carousel slide>
#for (int i = 0; i <= Model.Count()/3; i++ ) <!-- this works well, paging -->
{
<div class="item #if(i==0){<text>active</text>}">
#foreach(var well in Model)
{
<div class="span4">
<!-- some html here -->
</div>
}
</div>
}
</div>
Your inner foreach is iterating over the entire Model collection - you'll need to restrict it to just the relevant three items.
I'm guessing you want something like:
<div class="carousel slide>
#for (int i = 0; i <= Model.Count()/3; i++ ) <!-- this works well, paging -->
{
<div class="item #if(i==0){<text>active</text>}">
#foreach(var well in Model.Skip(i*3).Take(3))
{
<div class="span4">
<!-- some html here -->
</div>
}
</div>
}
</div>
One way to think about this problem is to split the original collection into groups for each 3 sequential elements. Fortunately, you can use the GroupBy LINQ method using the "element index divided by 3" as key. IMO, the advantage of this solution is that it expresses more clearly your intent and has better performance than reiterating the collection with Skip(x * 3).Take(3) in the inner loop.
<div class="carousel slide>
#foreach (var group in Model.Select((x, index) => new { element = x, index }).GroupBy(x => x.index / 3, x => x.element))
{
<div class="item #if( group.Key == 0) {<text>active</text>}">
#foreach(var well in group)
{
<div class="span4">
<!-- some html here -->
</div>
}
</div>
}
</div>
I would even change the model type to ILookup<int, TElement> and perform the grouping in the controller.

Dynamically create divs with incremented id using jquery

I have a text
I want to append multiple divs but the id should be changed dynamically.
e.g:
<div id=first>text</div>
<div id=first0>text</div>
<div id=first1>text</div>
<div id=first2>text</div>
<div id=first3>text</div>
<div id=first4>text</div>
<div id=first5>text</div>
Any help? thanks..
Your title and question content seem to disagree with each other. I am assuming you are wanting to create div's where each id sequentially increments each time one is created?
$(function(){
var count = 0;
$('#append').click(function(){
$('#parent').append('<div id="first'+count+'">text</div>');
count++;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="append">Add DIV</a>
<div id="parent"></div>
You can change it with .attr():
$('#first').attr('id', 'first6')
try to change the code to :
<div id="first">text</div>
<div id="first0">text</div>
<div id="first1">text</div>
<div id="first2">text</div>
<div id="first3">text</div>
<div id="first4">text</div>
<div id="first5">text</div>
dont forget the id inside " " (id="first" not id=first)
now you can simply use jquery : $("#first").attr('id','first6');
If you were interested in how styling works.`
$(function(){
for (let i = 0 ;i < 10; i ++){
$('#foo').append('<div id="first'+i+'">text</div>');
}
});
#first4 {
background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo"></div>
Create a div with id=Container and the create 5 divs dynamically using following code
//using JavaScript
for(var i=0;i<5;i++){
var obj = document.getElementById("container");
obj.innerHTML += '<div id="first'+i+'"></div>';
}

Resources