Table Column NO for Local Variable in MVC View Page - asp.net-mvc

Possible for my question?
My Application in Product Table.
No Name
1 Apple
2 Orange
3 PipleApple etc....
This above table of No is Sorted By Product ID.But my user is wanted some time prodect table in above product item deleted.Then ID is not ascending.Therefore Number ascending for want local variable in MVC Table of Index.chtml.Possible ....?
#model IEnumerable<RenewableEnergyProjcet.Models.REHPDataModels.ProductTable>
<table >
<thead>
<th>
#Html.Label("No")
</th>
<th>
#Html.Label("Name")
</th>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
//this part for dynamic local variable assign
</td>
<td >
#Html.DisplayFor(modelItem => item.Name)
</td>
}
</tbody>
</table>

Related

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

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>

Slow view building

I am in the process of converting an old asp.net forms application to MVC. I have run into a snag where I am displaying results of a search.
I have an entity framework model of Classes, Class Details, Rosters and Class instructors.
I have a search page where I wish to display all of the class details by class selected.
View:
#using System.Diagnostics
#model IEnumerable<SafetyReports.Models.DataModel.ClassDetails>
<div>
<table>
<tr>
<th>
Class Date
</th>
<th>
Location
</th>
<th>
Region
</th>
<th>
# of Attendees
</th>
</tr>
#foreach (var c in Model)
{
Debug.WriteLine(c.ClassDetailID);
<tr>
<td>
#c.ClassDate
</td>
<td>
#c.Location
</td>
<td>
#c.Region
</td>
<td>
#c.ClassRosters.Count
</td>
<td>
<input type="button" value="Detail" onclick="alert(#c.ClassDetailID)"/>
</td>
</tr>
}
</table>
</div>
controller:
public PartialViewResult SelectCourse(string id)
{
var e = new Entities();
var i = e.ClassDetails.Where(x => x.ClassID.ToString() == id).ToList();
return PartialView("_ClassesDetail", i);
}
My problem is that it seem to take about 1 second per 2-3 classes. I have one class type that has 1300 records and it takes about 5-6 minutes to return the view. What am I doing wrong? In the asp.net forms application I have a gridview that returns the same amount of data in seconds if that long. It isnt using EF though, just a sqldatasource. Could this be lazy loading?
firstly, as seen before using a int instead of a string for 'id' will speed up your EF query.
Here's one way to speed up the view rendering (or at least make it cleaner):
In view replace all the #foreach loop by a simple :
//here Razor will automatically repeat you model displayTemplate for
//each element of your IEnumerable
#Html.DisplayForModel()
And then define a Display Template for your model
~/Views/Shared/DisplayTemplates/ClassDetails.cshtml
#model SafetyReports.Models.DataModel.ClassDetails
<tr>
<td>
#Model.ClassDate
</td>
<td>
#Model.Location
</td>
<td>
#Model.Region
</td>
<td>
#Model.ClassRosters.Count
</td>
<td>
<input type="button" value="Detail" onclick="alert(#Model.ClassDetailID)"/>
</td>
</tr>
I found my issue, I had related objects being lazy loaded, I fixed the issue with including those objects with:
var i = e.ClassDetails.Where(x => x.ClassID == id).Include(x=>x.ClassInstructors).Include(x=>x.ClassRosters).ToList();

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>

MVC checkbox event in grid

I am a new bee in MVC world. I have a scenario like, i have an grid with a checkboxs as one column. When i click on checkbox an event would fire and it would update some values in database.
I am using Razor engine.
<table>
<tr>
<th>
ID
</th>
<th>
PName
</th>
<th>
PDescription
</th>
<th>
PSerialNo
</th>
<th>
PPrice
</th>
<th>
PActive
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#item.ID
</td>
<td>
#Html.ActionLink(#item.PName, "Edit", new { id = #item.ID })
</td>
<td>
#item.PDescription
</td>
<td>
#item.PSerialNo
</td>
<td>
#String.Format("{0:c}", item.PPrice)
</td>
<td>
#Html.CheckBox("chkActiveItem", item.PActive)
</td>
</tr>
}
here the content is showed in the grid. Here when i click on this check box i want to update the flag in the database.
How will i do it?
Please help.
foreach doesn't work in this case. You need to use for loop and then changes will get picked up on server side on post.
if you want to do ajax call then simply store id of each item within its row and pass that as parameter during ajax call to identify checked item on server side.

Resources