how to use a foreach in a view page - c#

Trying to pass a list of confirmed orders to the supplier page (checked with breakpoint the list is being past) just having problems using a foreach to display the list in the view.
//SupplierController
public ActionResult Index()
{
BuyABicycle_Entities db1 = new BuyABicycle_Entities();
IEnumerable<BicycleOrder> All_Orders = (from c in db1.BicycleOrders
where c.Id >= 1
select c).ToList();
SupplierVM model = new SupplierVM { allOrders = All_Orders };
return View(model);
}
//SupplierVM
public class SupplierVM
{
public IEnumerable<BicycleOrder> allOrders { get; set; }
}
Views/Supplier/Index
#model BicycleShop.ViewModels.SupplierVM
#{
ViewBag.Title = "Supplier";
//var orders = (IList<BicycleOrder>) Model.;
// var orders = (List<BicycleOrder>) Model.Order);
}
#using (Html.BeginForm())
{
<table>
#foreach (var _Order in Model.allOrders)
{
<text>
<tr>
<td>#_Order.CustomerName</td>
</tr>
</text>
}
</table>
<input type="submit" />
}
This throws the error with #foreach (var _Order in Model.allOrders)
Compiler Error Message: CS0012: The type 'IdeaBlade.EntityModel.Entity' is defined in an assembly that is not referenced. You must add a reference to assembly 'IdeaBlade.EntityModel, Version=6.1.7.0, Culture=neutral, PublicKeyToken=287b5094865421c0'.
Foreach loop for tables in MVC4
do I need to declare a variable for the list at the top and then run through that
any help appreciated. thanks

Your view specifies the model as an IEnumerable<SupplierVM>. So to iterate over the orders, you would first have to iterate over the suppliers:
#foreach (var supplier in Model)
{
foreach (var order in supplier.allOrders)
{
...
}
}
However, it seems you're not actually passing many SupplierVM instances, but just one. Therefore, you should change the view's model to:
#model BicycleShop.ViewModels.SupplierVM
And, then you can directly iterate over the orders:
#foreach (var order in Model.allOrders)
{
...
}

maybe razor is getting confused between HTML and code:
try this:
#foreach (var _Order in Model)
{
<text>
<tr>
<td>#Html.TextBoxFor(x => x.allOrders)</td>
<td>#_Order.allOrders</td>
#<td>#Html.TextBoxFor(x => x.CustomerName, new { #readonly = true }) </td>
#foreach(var item in _Order)
{
item.ItemProp <br />
}
</tr>
</text>
}
inside the {} razor is expecting it all the be code, if you want to put HTML in there - multi-line use <text></text> for one line use #:

Related

Get data to MVC 4 View from SQL Table with using Split() method

My question would be complicated but I will try to ask clearly.
I want to keep tags in a data at SQL, as using the comma in a cell. Then, try to call two different way on my MVC 4 project. One way (basic one) is working. But another one is hard to run. I will give my codes one by one.
SQL Table:
Controller:
public ActionResult Portfolio()
{
return View(db.Portfolios.ToList());
}
View:
#model IEnumerable<emirhanozkan.Models.Portfolio>
#{
foreach (var pot in Model)
{
<p>#pot.Tags.Split(',')[0]</p>
<p>#pot.Tags.Split(',')[1]</p>
<p>#pot.Tags.Split(',')[2]</p>
}
}
In same View one more foraech is running:
#foreach (var po in Model)
{
<li class="item #po.Tags.ToLower().Trim().Replace(",","")">
<img src="#po.Image" />
#po.Title
</li>
}
So, What I want to do with Split method:
<p>AngularJS</p>
<p>MVC</p>
<p>C#</p>
<p>Wordpress</p>
<p>MVC</p>
I guess my #pot.Tags.Split(',')[0], #pot.Tags.Split(',')[1] and #pot.Tags.Split(',')[2] code is wrong to list them but my brain not working anymore than that one. Please help me get them like my dream. Also, if you now to get just one from repeat words like <p>MVC</p> <p>MVC</p>, to just <p>MVC</p> please add to new code.
You can just loop on the array returned by Split() method and then render the tags:
foreach (var pot in Model)
{
var tags = #pot.Tags.Split(',');
foreach(var tag in tags)
{
<p>#tag</p>
}
}
sorry for errors, I write without compile:
List<Portfolio> Model = new List<Portfolio>();
StringBuilder finaltags= new StringBuilder();
foreach (var pot in Model)
{
finaltags.AppendLine("<p>" + #pot.Tags.Split(',') + "</p>");
}
#Html.Raw(finaltags.ToString());
foreach (var pot in Model)
{
var tags = #pot.Tags.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);
foreach(var tag in tags)
{
<p>#tag</p>
}
}
foreach (var pot in Model)
{
<li>
<img src="#po.Image" />
<a href="#po.Url" target="_blank" class="md-button md-primary">
#po.Title</a>
</li>
}
Thank you for your suggestions. But I found the answer to my question:
#{
foreach (var pot in Model)
{
string str = pot.Tags;
string[] strT = str.Split(',');
foreach (var poo in strT) {
<p>#poo</p>
}
}
}

Show Unique values From Table in mvc

I want to show unique values so that only unique link will be shown. what can i do please tell as soon as possible
[Here i want to show unique Value but here is repeatetion of values][1]
My controller is this
public ActionResult AdminStudentAttendance()
{
AdmissionDBEntities obj = new AdmissionDBEntities();
var v = obj.AddTables.ToList();
return View(v);
}
My View is this
#model IEnumerable<AMSPractice.Models.AttendanceIn>
#using AMSPractice.Models;
<h2>ShowStudentAttendance</h2>
#foreach (var a in Model)
{
#Html.ActionLink(a.RollNo,"OneStudentDates","Attendance", new { nid = a.RollNo }, null) <br />
}
Inside of your foreach, it looks like the only property of the model you care about is the RoleNo. Explore something like this:
#foreach (var roleNo in Model.Select(m => m.RoleNo).Distinct())
{
#Html.ActionLink(roleNo,"OneStudentDates","Attendance", new { nid = roleNo }, null) <br />
}
You may need to play with that a bit, but basically you are appending a filter to Model to first give you only the RoleNo properties of each, then unique only.
Use Distinct to get unique values.
var uniques= Model.Select(x => x.RollNo).Distinct().ToList();
#foreach (var a in uniques)
{
#Html.ActionLink(a,"OneStudentDates","Attendance", new { nid = a }, null) <br />
}

C# MVC ViewBag error

I'm new to C# MVC so please be patient. I'm having some trouble displaying output for the ViewBag. The application is a Fantasy Football webpage so I have three tables (right now, more to come) one for the basic player info (dbo.Player), one for player background (dbo.PlayerBackground), and one is just a definition table for the team (dbo.Team).
In one of my pages I have a name search and search by position and want to return information across these three tables.
public ActionResult Index()
{
var players = (from p in db.Players
join pb in db.PlayerBackgrounds on p.playerId equals pb.playerID
join t in db.Teams on p.teamAbbre equals t.teamAbbre
select new { playerID = p.playerId, playerName = p.name, position = p.position,
height = pb.height, weight = pb.weight, college = pb.college, dob = pb.dob,
imageUrl = pb.imageUrl, years = pb.years,
teamName = t.name
}).ToList();
ViewBag.data = players;
return View();
}
The query works fine but in index.cshtml I keep getting errors.
#foreach (var player in ViewBag.data)
{
<tr class="success ui-dragable playerRow" style="display: none;">
<td>
<input type="checkbox" />
</td>
<td>
#Html.ActionLink( (string)player.playerName, "Details", new { id = player.playerID }, new { #class = "detailsLink" })
</td>
<td>
#player.teamName
</td>
<td>
#player.position
</td>....
From the research I've done, this seems like it should work. I've tried it both with and without the (string) cast. Without the cast it gives me red squiggly saying I should cast and when I do I get:
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'playerName'
As I step through, I can watch player and it has the various properties just as it should. Any idea what I'm doing wrong?
The problem is that you are using an anonymous object. I can't really take the credit for that finding though, the answer I found was right here (Go give him an up-vote). Refer to it for complete details.
Essentially, the short of the story is that Anonymous objects are emitted as internal by the compiler. This causes problems when trying to use them as Razor views because they are compiled into a separate assembly by the ASP.Net runtime (internal only allows access in the same assembly).
So, the solution is to define a view model:
public class PlayerViewModel
{
// Replace with the actual type of playerId
public int playerId { get; set; }
// etc...
}
And use it in your controller:
public ActionResult Index()
{
var players = (from p in db.Players
join pb in db.PlayerBackgrounds on p.playerId equals pb.playerID
join t in db.Teams on p.teamAbbre equals t.teamAbbre
select new PlayerViewModel { playerID = p.playerId, ... }).ToList();
return View(players); // Use the strongly-typed model property for your view
// instead of ViewBag.data (It's recommended)
}
And finally in your view:
#* At the beginning of your view *#
#model IEnumerable<PlayerViewModel>
...
#foreach (var player in #Model)
{
<tr class="success ui-dragable playerRow" style="display: none;">
<td>
<input type="checkbox" />
</td>
<td>
#Html.ActionLink(player.playerName, "Details", new { id = player.playerID }, new { #class = "detailsLink" })
</td>
<td>
#player.teamName
</td>
<td>
#player.position
</td>....

Object reference not set to an instance of an object when calling foreach loop when calling it in the cshtml [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
So I am very new to MVC and development. What I am trying to do is basically create a single row table view of an user so that I can edit and update on that single row. (I hope I am making sense)
So I am using MVC Razor View and I want to create a table and edit within that table without using the CRUD functionality that is already built into MVC. I want to do inline editing of my tabular view.
So I have create a view that also has a partial view. I have a Model and my Repository as well. WHen I run this, it says Object not set to an instance of an object, and it's talking about my foreach loop. Here is the data below. If someone can offer assistance, that would be greatly appreciated. I am very new and only been really coding for about 3 months now.
Controller
public ActionResult Index(string id)
{
var attendee = (Guid)Membership.GetUser(id).ProviderUserKey;
CatalogAttendeeModel model = new CatalogAttendeeModel();
model.getAttendeesList(attendee);
var catalog = from ca in db.text
join a in db.text on ca.textID equals a.textID
where ca.textID== attendee
select ca;
foreach (var item in catalog)
{
if (item.IsActive == null)
{
item.IsActive = true;
}
}
return PartialView();
}
Index View
#model ODT4.Models.CatalogAttendeeModel
<table>
<thead>
<tr>
<th>
#Html.Label("Catalog")
#Html.Label("Role")
#Html.Label("Is Admin")
#Html.Label("Certificate Printed")
#Html.Label("Percent Watched")
#Html.Label("Percent Updated")
#Html.Label("PI Name")
#Html.Label("Action")
</th>
</tr>
</thead>
<tbody>
#foreach (var row in Model.catalogAttendees) //this is where I get the errormessage
{
{
Html.RenderPartial("CatalogAttendeeSingleRow", row);
}
}
</tbody>
</table>
Partial View
#model ODT4.Models.CatalogAttendeeModel
#{
<div id = "crud_tableEdit" >
<table>
<tr>
<td>#Html.DisplayFor(m => m.Catalog.CatalogCode)</td>
<td>#Html.DisplayFor(m => m.StudyRole)</td>
<td>#Html.DisplayFor(m => m.IsAdmin)</td>
<td>#Html.DisplayFor(m => m.IsTrainingDocPrinted)</td>
<td>#Html.DisplayFor(m => m.PercentWatched)</td>
<td>#Html.DisplayFor(m => m.PercentUpdatedOn)</td>
<td>#Html.DisplayFor(m => m.PIName)</td>
</tr>
</table>
</div>
}
CatalogModel
public List<Catalog_Attendee> catalogAttendees { get; set; }
public CatalogAttendeeModel()
{
}
public void getAttendeesList(Guid id)
{
//catalogAttendees = new List<Catalog_Attendee>();
CatalogAttendeeRepository rep = new CatalogAttendeeRepository();
catalogAttendees = rep.getAttendeesAll();
}
Repository
public List<Catalog_Attendee> getAttendeesAll()
{
return db.Catalog_Attendee.ToList();
}
public Catalog_Attendee getAttendeeByID(Guid id)
{
return db.Catalog_Attendee.FirstOrDefault(i => i.AttendeeID == id);
}
Here is the error message:
Server Error in '/' Application.
________________________________________
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 17: </thead>
Line 18: <tbody>
Line 19: #foreach (var row in Model.catalogAttendees)
Line 20: {
Line 21: {
Model.catalogAttendees is null - hence the exception.
As to why ... maybe the linq expression is not returning what it should. I'm not seeing what db.text should be, but then again I'm not seeing all the code.
Edit: Also, you're not returning the model to the view. return PartialView(model);

How can i count loops in mvc3 in #foreach

How can i close <tr> and open <tr> after 3 loop iterations? I have MVC 3 in .NET 4.0. How can I count loop iterations in MVC 3?
Current Code:
#foreach (var articleOnFirstPage in Model.ArticlesOnFirstSite)
{
<tr>
<td><div class="productsFrame"></div></td>
</tr>
}
I want to get this:
<tr>
<td><div class="productsFrame"></div></td>
<td><div class="productsFrame"></div></td>
<td><div class="productsFrame"></div></td>
</tr>
<tr>
<td><div class="productsFrame"></div></td>
<td><div class="productsFrame"></div></td>
<td><div class="productsFrame"></div></td>
</tr>
<tr>
<td><div class="productsFrame"></div></td>
<td><div class="productsFrame"></div></td>
<td><div class="productsFrame"></div></td>
</tr>
You could perform the following pornography in your view:
#model IEnumerable<Foo>
<table>
#foreach (var item in from i in Model.Select((value, index) => new { value, index }) group i.value by i.index / 3 into g select g)
{
<tr>
#foreach (var x in item)
{
<td><div class="productsFrame">#x.SomeProperty</div></td>
}
</tr>
}
</table>
or simply use view models and do the grouping in your controller action which obviously is what I would recommend you. The sole fact that you need to do this means that your view model is not adapted to your view's requirements which is to group results by 3. So adapt it. Don't pass IEnumerable<Foo> to your view. Pass IEnumerable<MyViewModel> where obviously MyViewModel will contain the necessary grouping so that in your views you could simply loop or since I hate writing for and foreach loops in views simply use display templates. They will take care of everything and your view will simply look like this:
<table>
#HtmlDisplayForModel()
</table>
Looks better than the initial pornography isn't it?
As requested in the comments section here's how I would implement this using view models.
As always in an ASP.NET MVC application you start by defining the view models that will reflect the requirements of your view (which I repeat are: show a table with 3 columns):
public class ItemViewModel
{
public string Title { get; set; }
}
public class MyViewModel
{
public IEnumerable<ItemViewModel> Items { get; set; }
}
then you move on to the controller that will fill and pass this view model to the view:
public class HomeController : Controller
{
public ActionResult Index()
{
// Obviously in a real world application the data is your domain model
// and comes from a repository or a service layer depending on the complexity
// of your application. I am hardcoding it here for the
// purposes of the demonstration
var data = Enumerable.Range(1, 30).Select(x => new { Title = "title " + x });
var model =
from i in data.Select((value, index) => new { value, index })
group i.value by i.index / 3 into g
select new MyViewModel
{
Items = g.Select(x => new ItemViewModel { Title = x.Title })
};
return View(model);
}
}
and finally you write the corresponding view (~/Views/Home/Index.cshtml):
#model IEnumerable<MyViewModel>
<table>
#Html.DisplayForModel()
</table>
and the ~/Views/Home/DisplateTemplates/MyViewModel.cshtml display template:
#model MyViewModel
<tr>
#Html.DisplayFor(x => x.Items)
</tr>
and finally the corresponding ~/Views/Home/DisplateTemplates/ItemViewModel.cshtml display template:
#model ItemViewModel
<td>#Html.DisplayFor(x => x.Title)</td>
and that's pretty much it. Simple, clean, following good practices and conventions.
Obviously to bring this a step further you would introduce AutoMapper to perform the actual mapping between your domain models and view models and you will end up with a very elegant solution that will look like this:
public ActionResult Index()
{
IEnumerable<DomainModel> data = ...
var viewModel = Mapper.Map<IEnumerable<DomainModel>, IEnumerable<MyViewModel>>(data);
return View(viewModel);
}
or a step further:
[AutoMap(typeof(IEnumerable<DomainModel>), typeof(IEnumerable<MyViewModel>))]
public ActionResult Index()
{
IEnumerable<DomainModel> data = ...
return View(data);
}
Now we are starting to get into serious business.
The first thing that comes to mind is Phil Haack's better foreach loop
Using it you gain an index and can use it like
<ol>
#Model.Each(#<li>Item #item.Index of #(Model.Count() - 1): #item.Item.Title</li>)
</ol>
What you're specifically looking for should be something like:
#Model.ArticlesOnFirstSite.Each(#<td><div class="productsFrame"></div></td>#(#item.Index % 3 == 0 ? "</tr><tr>" : ""))
Something like this may work.
#{int i = 0;}
#foreach (var articleOnFirstPage in Model.ArticlesOnFirstSite)
{
#if ((i++ % 3) == 0) {
#if (i != 1) {
#:</tr>
}
#:<tr>
}
#:<td><div class="productsFrame"></div></td>
}
#if (i != 0) {
#:</tr>
}
And this is a brute force solution to your problem.
As other have suggested and as I suggest, you should change your methodology: use view models, group items by 3.
About how to correctly use model-view-controller pattern you can look on the web.
http://msdn.microsoft.com/en-us/library/gg416514(v=vs.98).aspx is a good start.
Like the others said, the best and prettiest solution is probably to do the grouping in the controller, but this might get the job done:
#for (int i = 0; i < Model.ArticlesOnFirstSite.Count; i += 3)
{
<tr>
#foreach (Article article in Model.ArticlesOnFirstSite.Skip(i).Take(3))
{
<td>#article.Title</td>
}
</tr>
}

Categories