View renders but doesn't update - asp.net-mvc

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>

Related

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";
}

Redirect to Link from Link saved in database

I am developing an Web Application in MVC.
I have added a link in Database eg:"www.google.com", I want user to redirect to Google on click.
I tried in many ways but getting the controller in address bar "eg:http://localhost:/Home/www.google.com"
View Code
<table class="table" style="margin-top:10px;">
<tr>
<th>
Sr. No.
</th>
<th>
Name
</th>
<th>
Details
</th>
<th>
Status
</th>
<th>
Tracking No.
</th>
<th>
Tack Order
</th>
<th>
Ordered On
</th>
</tr>
#{int row = 0;} #foreach (var item in Model) { if (item.PaymentSuccessfull == true) {
<tr>
<td>
#(row += 1)
</td>
<td>
#Html.DisplayFor(modelItem => item.DeliveryDetail.FirstName)
</td>
<td>
Order Details
</td>
<td>
#Html.DisplayFor(modelItem => item.Status)
</td>
<td>
#Html.DisplayFor(modelItem => item.TrackingId)
</td>
<td>
//Here I want user to redirect to Google on click
Track
</td>
<td>
#Html.DisplayFor(modelItem => item.DateTime)
</td>
</tr>
} }
Help is appreciated.
Thanks in advance.
You must store the protocol with the rest of the URL.
so:
www.google.com
becomes:
http://www.google.com

Do a sum in View based on criteria

I have a Model and view something like this:
Model and View
I want to display the sum/total based on either State or Federal (see the green box in the image).
Here is my view so far.
#if (Model.Any())
{
<table>
<tr>
<th>
</th>
<th>
STATE
</th>
<th>
FEDERAL
</th>
</tr>
<tr>
<th width="180">
Project
</th>
<th width="120">
Sponsor
</th>
<th width="120">
Sponsor
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(m => item.Project)
</td>
#if (item.Provider == "State")
{
<td>
#Html.DisplayFor(m => item.Sponsor)
</td>
<td>
</td>
}
else
{
<td>
</td>
<td>
#Html.DisplayFor(m => item.Sponsor)
</td>
}
</tr>
}
</table>
}
How can I display the value (in the green box)? I know that doing a sum in View is not a good practice.
You can use linq with this.
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(m => item.Project)
</td>
#if (item.Provider == "State")
{
<td>
#Html.DisplayFor(m => item.Sponsor)
</td>
<td>
</td>
}
else
{
<td>
</td>
<td>
#Html.DisplayFor(m => item.Sponsor)
</td>
}
</tr>
}
<tr>
<td></td>
<td>#Model.Where(x => x.Provider == "State").Sum(x => x.Sponsor)</td>
<td>#Model.Where(x => x.Provider != "State").Sum(x => x.Sponsor)</td>
</tr>

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>

Is there a way to make the MVC Html.Actionlink invisible based on a property value

Hi say I have a view containing:
<table>
<tr>
<th>
Invoice Number
</th>
<th>
Invoice Date
</th>
<th>
Organisation
</th>
<th>
Region
</th>
<th>
Total Excluding GST
</th>
<th>
Total Including GST
</th>
<th>
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.InvoiceNumber)
</td>
<td>
#Html.DisplayFor(modelItem => item.InvoiceDate, "{0:D}")
</td>
<td>
#Html.DisplayFor(modelItem => item.Organisation.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Area.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalExcludingGst)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalIncludingGst)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id })
</td>
</tr>
}
</table>
and each item has a boolean property Editable. Is there a way to make the Edit link visible/invisible based on the value of this property?
You can use an if:
#if (item.Editable) {
#Html.ActionLink(...)
}
Basically you can use css. But if you insist in actionlink
Html.ActionLink(
"LinkName",
"Action",
null,
new { #style = "display:none" });
So if you use it with #if statement in view, you can achieve what you want.
Use CSS
I am invisible

Resources