How to retrive data from ViewData in foreach loop - asp.net-mvc

Hi I have get records in ViewData["TutorList"] = GetData.Tables[0];
Here GetData DataSet and in that table[0] is bind with ViewData["TutorList"]
#foreach (var item in (DataTable)ViewData["TutorList"])
{
<tr>
<td>#item["Name"].ToString()</td>
</tr>
}
How Can I get all the records in foreach

The following code should give you want you want
#foreach (DataRow item in ((DataTable)ViewData["TutorList"]).Rows)
{
<tr>
<td>#item["Name"].ToString()</td>
</tr>
}
But I would suggest that you use a view model specific to the view and use the associated Html helpers in your view.

Related

How to get total of a rows value in a asp.net mvc table

I want to show total in a new row in a table. How to write a for loop and show grand total in the next row?
Datatype - consumption is double
<tbody>
#if (ViewBag.Total_PowerList != null)
{
Double value = 0;
foreach (var item in ViewBag.Total_PowerList as List<TransformerEnergyMonitoring.Models.KWHConsumption>)
{
<tr>
<td>#item.timeStamp</td>
<td>#item.finalState</td>
<td>#item.consuption.ToString("0.00")</td>
</tr>
}
foreach (var item in ViewBag.Total_PowerList as List<TransformerEnergyMonitoring.Models.KWHConsumption>)
{
value = item.consuption++;
}
<tr>
<td>#value</td>
</tr>
}
</tbody>
Instead of using Viewbag use Model reference in view and use
<td>#Model.Sum(i => i.consuption)</td>
If you are using viewbag then write the sum of consuption code done in controller side only and assign that sum of value in different viewbag and use it in view directly.

Rendering dynamic HTML content into variable in Razor

I want to render dynamic HTML content into a variable to later pass it to a function. The reason is, that I receive the actual content through another platform and I can print it out using an internal function. This function accepts values to put into placeholders.
Example:
This is my content:
{mytable}
Blah
Now I can set any content on mytable. Good.
The content I want to put there is currently the following.
#using System.Data
#model Dictionary<string, DataView>
<table>
#foreach (var group in Model)
{
<tr>
<th colspan="3">#group.Key</th>
</tr>
foreach (DataRowView data in group.Value)
{
<tr>
<td>#data.Row["col1"]</td>
<td>#data.Row["col2"]</td>
<td>#data.Row["col3"]</td>
</tr>
}
}
</table>
Well. What is the best way to actually render the above output into a variable? I firstly thought of just appending every HTML line into a string, but it sounds rather inefficient and weak to me.
I'm not an expert in ASP.NET, I come from PHP and I know a bit about output buffers. Is this a possible way? What would you recommend?
You could create a ViewHelper:
#helper RenderTable(Dictionary<string, System.Data.DataView> model)
{
<table>
#foreach (var group in Model)
{
<tr>
<th colspan="3">#group.Key</th>
</tr>
foreach (System.Data.DataRowView data in group.Value)
{
<tr>
<td>#data.Row["col1"]</td>
<td>#data.Row["col2"]</td>
<td>#data.Row["col3"]</td>
</tr>
}
}
</table>
}
Then call it:
#{
string output = RenderTable(someData).ToHtmlString();
}
Create strongly typed partial view and add this partial view in actual view
See below link
http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC

Why is DisplayForModel not showing the expected output?

I have a simple razor page, where the first part correctly shows information from each element in my model in a table. After that I'm trying to use DisplayFor to show me every property in the model, but it generates 123 for each iteration. What am I doing wrong?
#model System.Data.Entity.IDbSet<DBFramework.MyPerson>
#{
ViewBag.Title = "Home Page";
}
<p></p>
<table border="1">
<thead>
<tr>
<th style="width:300px;">Name</th>
<th style="width:300px;">Age</th>
</tr>
</thead>
#foreach (var p in Model)
{
<tr>
<td>#Html.Label(p.Name)</td>
<td>#Html.Label(p.Age.ToString())</td>
</tr>
}
</table>
#foreach (DBFramework.MyPerson x in Model)
{
#Html.DisplayForModel(x)<br/>
}
Creates the following output:
Name Age
Mike 40
Penny 1
Kunal 30
123
123
123
Try this:
#foreach (var item in Model)
{
#Html.DisplayFor(model => item)
}
DisplayFor and DisplayForModel are used with templates in Views\Shared\DisplayTemplates.
When you call DisplayForModel, it will take the page model (Model) and if you specify a parameter, will try to load the template with the name provided (if string) or add the object as additional data in ViewData. When you called:
#foreach (DBFramework.MyPerson x in Model)
{
#Html.DisplayForModel(x)<br/>
}
You told it - Display Model which is type System.Data.Entity.IDbSet<DBFramework.MyPerson> and add MyPerson in the template ViewData. Do this 3 times. You also didn't give him a hint to select a DisplayTemplate and it will try to use convention to find it.
As Bappi Datta said, if you want to display a template for MyPerson, you need to call:
#foreach (var item in Model)
{
#Html.DisplayFor(model => item)
}
However, you need a MyPerson display template created which accepts #model DBFramework.MyPerson to render the item or it will simply do a .ToString() by default I believe.

pass model to view using viewbag

i want to send two model in my view
and in other page maybe and need more
controller
ViewBag.Users = db.Users.ToList();
and in View
#using Documentation.Models
#{
var Users = (Documentation.Models.User)ViewBag.Users;
}
<table>
#foreach (var item in Users) {
<tr>
<td>
#Html.DisplayFor(item.username)
</td>
</tr>
}
</table>
but i get no answer , i know my code is not right
i got this error
foreach statement cannot operate on variables of type 'Documentation.Models.User' because 'Documentation.Models.User' does not contain a public definition for 'GetEnumerator'
you should cast viewbag.model to List of User. Not to User.
#{
var Users = (List<User>)ViewBag.Users;
}

Distinct in Ienumerable model

I have a IEnumerable ViewModel. This has the same names and I want to select only one name and display it in the header. Please suggest.
My code is a below. I am trying to use distinct but it does not work
#foreach (var item in Model.abc)
{
{
<table>
<tr>
<td style="width:100px" >#Html.Label("Name")</td>
<td style="width:225px"> #Html.DisplayTextFor(model => item.Name.Distinct())</td>
</tr>
</table>
}
}
To be more specific: my result will have only names {a,a,a,a,a} so I just want to pick the single {a} and display it.
This has the same names and i want to select only one name and display
it in the header.
If they all are the same and assuming Name implements IEnumerable you could try First:
Update: add a new property to your model
otherProperty = item.Name.First();
Then:
#Html.DisplayTextFor(model => item.otherProperty)
use this code in your app
#foreach (var item in Model.Select(x => x.Title).Distinct())
{
<option>#item</option>
}
you can use #html.display(X=>item) instead #item

Resources