How to write if statement on model.property - asp.net-mvc

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)

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

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

Display a string in place of a NULL date

when the date property is set in the model, this is executed:
if (!(rdr["JobEnded"] is DBNull)) { job.JobEnded = (DateTime)rdr["JobEnded"]; }
which results in job.JobEnded being 1/1/0001 12:00:00 AM.
What can I use in place of
#Html.DisplayFor(modelItem => item.JobEnded)
to show "In Progress" instead of "1/1/0001 12:00:00 AM".
I've tried putting an if/else in the view, but was only able to display a valid date or nothing. I'm sure I'm missing something really basic here as this is my first attempt at an APS.NET MVC view.
You're dealing with default dates here, so to test for it, you need to use:
#if (item.JobEnded == default(DateTime))
{
#:In Progress
}
else
{
#Html.DisplayFor(m => item.JobEnded)
}
Or in one-liner form:
#((item.JobEnded == default(DateTime)) ? "In Progress" : Html.DisplayFor(m => item.JobEnded).ToString())

Conditional inside foreach inside conditional with Razor

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

Html.Raw() in ASP.NET MVC Razor view

#{int count = 0;}
#foreach (var item in Model.Resources)
{
#(count <= 3 ? Html.Raw("<div class=\"resource-row\">").ToString() : Html.Raw(""))
// some code
#(count <= 3 ? Html.Raw("</div>").ToString() : Html.Raw(""))
#(count++)
}
This code part does not compile, with the following error
Error 18 Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'System.Web.IHtmlString' d:\Projects\IRC2011_HG\IRC2011\Views\Home\_AllResources.cshtml 21 24 IRC2011
What I must I do?
Html.Raw() returns IHtmlString, not the ordinary string. So, you cannot write them in opposite sides of : operator. Remove that .ToString() calling
#{int count = 0;}
#foreach (var item in Model.Resources)
{
#(count <= 3 ? Html.Raw("<div class=\"resource-row\">"): Html.Raw(""))
// some code
#(count <= 3 ? Html.Raw("</div>") : Html.Raw(""))
#(count++)
}
By the way, returning IHtmlString is the way MVC recognizes html content and does not encode it. Even if it hasn't caused compiler errors, calling ToString() would destroy meaning of Html.Raw()
The accepted answer is correct, but I prefer:
#{int count = 0;}
#foreach (var item in Model.Resources)
{
#Html.Raw(count <= 3 ? "<div class=\"resource-row\">" : "")
// some code
#Html.Raw(count <= 3 ? "</div>" : "")
#(count++)
}
I hope this inspires someone, even though I'm late to the party.
You shouldn't be calling .ToString().
As the error message clearly states, you're writing a conditional in which one half is an IHtmlString and the other half is a string.
That doesn't make sense, since the compiler doesn't know what type the entire expression should be.
There is never a reason to call Html.Raw(...).ToString().
Html.Raw returns an HtmlString instance that wraps the original string.
The Razor page output knows not to escape HtmlString instances.
However, calling HtmlString.ToString() just returns the original string value again; it doesn't accomplish anything.

Resources