Conditional inside foreach inside conditional with Razor - asp.net-mvc

I'm trying to learn Razor but have come up against a syntax problem. When I run the following code:
#if (searchTerm != ""){
<h2>Showing #ExamineManager.Instance.Search(searchTerm, true).Count() results for #searchTerm</h2>
<div class="search-results">
#foreach (var result in ExamineManager.Instance.Search(searchTerm, true)) {
if (result.Fields["nodeTypeAlias"] == 'File'){
<p>File</p>
}else{
<p>Not file</p>
}
}
</div>
}
this errors, saying "Too many characters in character literal". The actual mechanism of the code works, it's just a syntax problem with the way the conditionals are nested I think, but I've tried various combinations of #{} blocks and prepending # to various lines but just can't get it to work.
Can anyone see what I'm doing wrong?
Thanks!

'File' is invalid syntax. You probably meant "File" in your if condition when you want to work with strings:
if (result.Fields["nodeTypeAlias"] == "File")

That is because you've got comparison
result.Fields["nodeTypeAlias"] == 'File'
In c#, you cannot write strings into single quotes like that. You should change it into
result.Fields["nodeTypeAlias"] == "File"

Just change the Code In this way
#{
if (searchTerm != ""){
<h2>Showing #ExamineManager.Instance.Search(searchTerm, true).Count() results for #searchTerm</h2>
<div class="search-results">
foreach (var result in ExamineManager.Instance.Search(searchTerm, true)) {
if (result.Fields["nodeTypeAlias"] == 'File'){
<p>File</p>
}
else{
<p>Not file</p>
}
}
</div>
}
}
THis code will help you

Related

Angular2 How to Handle null value in .find methode

Here i wrote simple .find method its finding my data well but if that record is not available then its displying 1st Record
public _pagedItems : any;
someval(value){
if(value.length>=5){
this._pagedItems= this.allItems.find(e=>e.uniqueid = value);
this.pagedItems=[];
this.pagedItems.push(this._pagedItems);
}
if my uniquId is not avaible then its should display Nodata OR Null
Here is an example for handle null and undefined and return no data like
Your code is looking wrong. Your are code using (=) symbol. So change (==).
The difference between = and == (=) equal symbol is assigning value (==) symbol is comparing a value
For more proper example here,
public _pagedItems : any[]=[];
someval(value:string){
if(!this.isObjNull(value) && !this.isObjNull(this.allItems) && this.allItems.length>0){
if(value.length>=5){
if(this.allItems.find(e=>e.uniqueid ==value)){
//you need one data use find
this._pagedItems=this.allItems.find(e=>e.uniqueid == value);
//you need multiple data use filter
this._pagedItems = this.allItems.filter(e=>e.uniqueid == value);
}
//here Iam logged final variable
console.log("Final Paged Items",this._pagedItems);
}
}
}
//here is the method check null and undefined.
isObjNull(data){
If(data!=null && data!=undefined){
return true:
}else{
return false;
}
}
You can handle html part like this
<ng-container *ngIf="_pagedItems; else noData">
<ng-container *ngIf="_pagedItems.length>0; else noData">
<!-- do you logic here.like *ngFor -->
</ng-container>
</ng-container>
<ng-template #noData>
<p>No data found</p>
</ng-template>
That's all let try this once. If any error please let me know.
Note:- here Iam used sample variable please replace your actual variable.
For comparison related more thing visit here.
Difference between == and === in JavaScript

Displaying Html.ActionLink and text on the same line, if some conditions are true

How can I display an Html.ActionLink and a text on the same line with if's conditions? To be more clear, I have this piece of code:
#if (Model.StatusID == 3)
{
<p>#Html.ActionLink("A", "B", "CheckOut", new { id = Model.ID, returnUrl = Request.RawUrl }, null)</p>
}
and in the immediate right of the CheckOut link, I would like to have the word "abc" only if a condition is true.
I tried using <td></td>, but I doesn't let me, because all that is inside a div.
Also, I tried to use <text></text>, but without a good result.
Thank you!

How can I check string value in razor view?

I have a model, and I want to show dome data, if table_name == "something". All data are different, that is why I need to check. but when I write ==, my View does not understand. It returns only else clause data.
Please help me, how can I check the string value in view?
#foreach (var item in Model)
{
if (item.table_name == "tPerson")
{ #item.user_group_name: <strong>#item.user_full_name</strong> | #item.date;
}
else if (item.table_name == "tBook")
{ Book<strong>#item.book_name</strong> | #item.date}
else
{#item.table_name <strong>#item.name</strong> | #item.date}
}
Check out the "item.table_name" has the back Space or front space

MVC Parameterized Element

I have a partial view which I want to display a different type of HTML element based on a property of the model. The solution I've come up with is:
#if (Model.Element == "span")
{
<span>
Some stuff
</span>
}
else if (Model.Element == "p")
{
<p>
Some stuff
</p>
}
else
{
<div>
SomeStuff
</div>
}
This is obviously not very extensible. I'd like to write something like:
<#Model.Element>
Some stuff
</#Model.Element>
but it doesn't seem to be allowed. Is there any way to do something like this.
(I appreciate that this goes somewhat against the grain of MVC as the model and therefore the controller is specifying HTML. Actually, the element type comes from the parent view, so I think this is okay.)
Many thanks in advance.
#Html.Raw("<" + Model.Element + ">")
Some Stuff
#Html.Raw("</" + Model.Element + ">")
Is about as simple as possible. Though you could always use the TagBuilder:
#{
TagBuilder tb = new TagBuilder(Model.Element);
tb.InnerHtml = "Some Stuff";
#Html.Raw(tb.ToString())
}

How to write if statement on model.property

For the following 'if' statement I am getting an error:
"the name model does not exist in the current context".
#if (model.OrderStatusId == 1)
{
}
This line not shows an error:
#Html.DisplayNameFor(model => model.OrderStatusId)
What would be right syntax for the 'if' statement?
#if (Model.OrderStatusId == 1)
{
}
Uppercase M on Model
you need an to use an uppercase M for this to work
Model
In this statement, Model is a property of your View. Notice the uppercase M.
#if (Model.OrderStatusId == 1)
{
}
Here, model is an part of an expression method argument and can be named anything you want.
#Html.DisplayNameFor(model => model.OrderStatusId)

Resources