Model Null Exception --> #if(Model.Count() > 0 ) - asp.net-mvc

Here is the error i am facing. My code seems fine where i have two data models. Both models are stored in the database as table. Now in the CSHTML (Razor view page) it throws exception on
#foreach(var obj in Model)
But it says Model is Null. Please help to resolve the issue.
Here is the code:
enter image description here

#if (Model.Count() > 0)
{
<table class="table-bordered table-striped" style= "width: 100%">
<thead>
<tr>
<th>
Work Order Number
</th>
<th>
Part Number
</th>
<th>
Welding Points
</th>
<th>
Target Work Center
</th>
<th>
Operator
</th>
<th>
Standard Cycle Time
</th>
</tr>
</thead>
<tbody>
#foreach (var obj in Model)
{
<td width="30%">#obj.WorkOrderNumber</td>
<td width="30%">#obj.PartNumber</td>
<td width="30%">#obj.WeldingPoints</td>
<td width="30%">#obj.TargetWorkCenter</td>
<td width="30%">#obj.Operator</td>
<td width="30%">#obj.StandarCycleTime</td>
}
</tbody>
</table>
}
else
{
<p><strong>No Data to Display!</strong></p>
}

Related

The DataTables are not responsive and taking too long to load the data?

I have 1000 records and I am able to fetch them from database like in one second. But they are taking like 9s for rendering. It is maybe because of data tables or I am looping through each record to display. If I disable the data tables performance is better but still not good. Following is my code, could you give your suggestions. I am pretty sure it is slow because of rendering and scripting.
<div class="table-responsive">
<table class="display" id="list" cellspacing="0">
<thead>
<tr>
<th>
#Content.Code
</th>
<th> #Content.Name </th>
<th>
#Content.Location
</th>
<th>
#Content.Date
</th>
<th> #Content.Remaining </th>
<th>
#Content.Status
</th>
<th>
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#item.Code
</td>
<td>
#item.Name
</td>
<td>
#item.Location
</td>
<td>
#item.Date
</td>
<td>
#item.ReDays
</td>
<td>
#item.Status
</td>
<td width="15%">
<i class="fa fa-pencil" aria-hidden="true"></i>
#if (User.IsInRole("Admin") || ViewBag.HavePermission == true)
{
<i class="fa fa-trash" aria-hidden="true"></i>
}
</td>
</tr>
}
</tbody>
</table>
</div>
I don't want to use server-side processing because I don't know how to control this HavePermisson and UserRole part. Please, give a suggestion?

How to put a row in red with Razor in MVC5

I have a table, which I want to mark when the real stock is less than the minimum stock, for that I'm going to render the view using Razor, but I'm not getting results
I enclose my view that contains the table that we wish to intervene
<table class="table table-bordered table-hover table-condensed">
<tr>
<th>
#Html.DisplayNameFor(model => model.First().v_Nombre)
</th>
<th>
#Html.DisplayNameFor(model => model.First().StockReal)
</th>
<th>
#Html.DisplayNameFor(model => model.First().StockMinimo)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
var fila = string.Empty;
if(item.StockReal < item.StockMinimo)
{
fila = "danger";
}
<tr class="#fila">
<td>
#Html.DisplayFor(modelItem => item.v_Nombre)
</td>
<td>
#Html.DisplayFor(modelItem => item.StockReal)
</td>
<td>
#Html.DisplayFor(modelItem => item.StockMinimo)
</td>
<td>
Editar
</td>
</tr>
}
</table>
Expected behavior: that the row where the real stock is less than the minimum stock turns red
Behavior Achieved: No change in my view
what am I doing wrong? What is missing in my Razor code? any help for me?
In bootstrap 4 the class is table-danger so change your code:
if(item.StockReal < item.StockMinimo)
{
fila = "table-danger";
}

How come Thymeleaf tables creates a gap?

Why does my Thyme leaf creates such a big gap when I use the following code below.
BAD/WARNING
</h3>
<h3>
APPLICATION PROCESSING
</h3>
<table class="tablebad">
<tr>
<th> Status </th>
<th> HostName </th>
<th> Process Name </th>
<th> Process Count </th>
</tr>
<tr th:each="DartModel, iterStat : ${countlist}">
<td th:if ="${DartModel.Status == 'BAD'}" th:text="${DartModel.Status}"></td>
<td th:if="${DartModel.Status == 'BAD'}" th:text="${DartModel.host}"></td>
<td th:if="${DartModel.Status == 'BAD'}" th:text="${DartModel.processName}"></td>
<td th:if="${DartModel.Status == 'BAD'}" th:text="${DartModel.processCount}"></td>
</tr>
</table>
<h3>
APPLICATION PROCESSING
</h3>
<table class="tableok">
<thead>
<tr>
<th> Status </th>
<th> HostName </th>
<th> Process Name </th>
<th> Process Count </th>
</tr>
</thead>
<tbody>
<tr th:each="DartModel, iterStat : ${countlist}">
<td th:if="${DartModel.Status == 'OK'}" th:text ="${DartModel.Status}"></td>
<td th:if="${DartModel.Status == 'OK'}" th:text="${DartModel.host}"></td>
<td th:if="${DartModel.Status == 'OK'}" th:text="${DartModel.processName}"></td>
<td th:if="${DartModel.Status == 'OK'}" th:text="${DartModel.processCount}"></td>
</tr>
</tbody>
</table>
Result
and under this will be all my data printed out with the status of "OK". I am ultimately trying to sort the data based on value. I tried working with javascript/jquery but it was too complicated. I found a way to split the data rows based on values. If values is "BAD" display table above vice versa to values with "OK"
Am I doing this wrong? I
The problem is that your are putting the th:if on the <td> elements instead of the <tr>. This means that your html looks something like this (where you have several empty rows between each non-empty row.
<table>
<tr>
<td>BAD</td>
<td>...</td>
<td>...</td>
</tr>
<tr></tr>
<tr></tr>
<tr>
<td>BAD</td>
<td>...</td>
<td>...</td>
</tr>
<tr></tr>
</table>
You should just move the th:if to the <tr> element, like so:
<table class="tablebad">
<tr>
<th>Status</th>
<th>HostName</th>
<th>Process Name</th>
<th>Process Count</th>
</tr>
<tr th:each="DartModel, iterStat : ${countlist}" th:if="${DartModel.Status == 'BAD'}">
<td th:text="${DartModel.Status}" />
<td th:text="${DartModel.host}" />
<td th:text="${DartModel.processName}" />
<td th:text="${DartModel.processCount}" />
</tr>
</table>

How to handle errors in Razor view engine?

How to handle errors in Razor (i.e. Null Exception)other than (try/catch)?
I do not want to insert (try/catch) block in every razor block.
Example (The (Model.ListofEntity) or (Item.Values) may be null and an exception might occur):
<table class="table">
<tr>
<th>
<div id="chkCheckAll" class="divCheckbox">
</div>
</th>
#foreach (var column in Model.ListofEntity.Columns)
{
int t = Convert.ToInt32(" ");
<th>
#column
</th>
}
</tr>
#foreach (CVMEntities.Item Item in Model.ListofEntity.Items)
{
<tr>
<td>
<div id=#Item.ID class="divCheckbox">
</div>
</td>
#foreach (var Value in Item.Values)
{
<td>
#Value
</td>
}
</tr>
}
</table>

View renders but doesn't update

Ive got a view that i pass a model to. I want it to render out a table out of a list in the model. It does that just fine it renders the view in that case that if i debugg it i can se the values goes to the right places. But the browser never updates. If i press ctrl+R in the browser it renders the view again in the exact same way but this time it show the list in the browser. Can i make this update everytime the view renders so that i don't have to press ctrl+R?
[HttpPost]
public ActionResult CreateResource(ViewModel view)
{
view.ResourceDateCreated = DateTime.Now;
viewmodel = view;
Session["ViewModel"]=viewmodel;
return View("Index", viewmodel);
}
The view:
<table class="table">
<tr>
<th>
MetaDataName
</th>
<th>
MetaDataDescription
</th>
<th>
LanguageCode
</th>
<th>
UserId
</th>
<th>
MetaDataDateCreated
</th>
#*<th>#Html.DisplayNameFor(model => model.MetaList)</th>*#
</tr>
#if (Model.Metadata.Count != 0)
{
foreach (var item in Model.Metadata)
{
<tr>
<td>
#item.MetaDataName
</td>
<td>
#item.MetaDataDescription
</td>
<td>
#item.LanguageCode
</td>
<td>
#item.UserId
</td>
<td>
#item.MetaDataDateCreated.ToString()
</td>
<td>
<table>
<tr>
<th>
MetaListId
</th>
</tr>
#if (item.MetaList.Count != 0)
{
foreach (var list in item.MetaList)
{
<tr>
<td>
#list.MetaListId
</td>
</tr>
}
}
</table>
</td>
</tr>
}
}
</table>

Resources