Sequence contains no elements errror - MVC (Models and Data Access) - asp.net-mvc

I'm trying to follow the MVC Music Store tutorial , but I got an error which I can't handle. I've created the action:
public ActionResult Browse(string category)
{
using (OnlineStoreDbContext db = new OnlineStoreDbContext())
{
// Get category and its associated products
var categoryModel = db.Categories.Include("Products")
.Single(c => c.Title == category);
return View(categoryModel);
}
}
Than I created and the respective View:
#model OnlineStoreMVC.Core.Models.Category
#{
ViewBag.Title = "Browse";
}
<h2>Browse Category: #Model.Title</h2>
<ul>
#foreach (var product in Model.Products)
{
<li>
#product.Title
</li>
}
</ul>
But when I try to open: http://localhost:51642/Store/Browse?cat=Action, I get error:
"Sequence contains no elements" regarding this line:
var categoryModel = db.Categories.Include("Products")
.Single(c => c.Title == category);
I've alredy tried to replace Single with SingleOrDefault, but then the error was
"Object reference not set to an instance of an object." regarding that line in the View: "<h2>Browse Category: #Model.Title</h2>"

The problem is that you're passing cat as key in you're url and it should be category. So you should call http://localhost:51642/Store/Browse?category=Action
About the the error "Object reference not set to an instance of the object" you have to change you Action method to:
public ActionResult Browse(string category)
{
using (OnlineStoreDbContext db = new OnlineStoreDbContext())
{
// Get category and its associated products
var categoryModel = db.Categories.Include("Products")
.SingleOrDefault(c => c.Title == category);
if (categoryModel == default(Category))
{
categoryModel = new Category();
categoryModel.Products = new List<Product>();
}
return View(categoryModel);
}
}

Related

"Object Does not Contain definition for Obtained" ASP.Net MVC [duplicate]

can someone tell me what I'm doing wrong? :-)
I have this simple query:
var sample = from training in _db.Trainings
where training.InstructorID == 10
select new { Something = training.Instructor.UserName };
And I pass this to ViewBag.
ViewBag.Sample = sample;
Then I want to access it in my view like this:
#foreach (var item in ViewBag.Sample) {
#item.Something
}
And I get error message 'object' does not contain a definition for 'Something'. If I put there just #item, I get result { Something = SomeUserName }
Thanks for help.
This cannot be done. ViewBag is dynamic and the problem is that the anonymous type is generated as internal. I would recommend you using a view model:
public class Instructor
{
public string Name { get; set; }
}
and then:
public ActionResult Index()
{
var mdoel = from training in _db.Trainings
where training.InstructorID == 10
select new Instructor {
Name = training.Instructor.UserName
};
return View(model);
}
and in the view:
#model IEnumerable<Instructor>
#foreach (var item in ViewBag.Sample) {
#item.Something
}
If you want to send in ViewData For example and don't want to send in model
you could use the same could as in the upper answer
and in the Controller
enter code here
ViewData[Instractor] = from training in _db.Trainings
where training.InstructorID == 10
select new Instructor {
Name = training.Instructor.UserName
};
and in the view you need to cast this to
`IEnumerable<Instructor>`
but to do this you should use
#model IEnumerable<Instructor>
Then you could do something like this
IEnumerable<instructors> Instructors =(IEnumerable<Instructor>)ViewData[Instractor];
then go with foreach
#foreach (var item in Instructors ) {
#item.Something
}

Assign Path for each link in ASP MVC

I have multiple pdf documents that I have to show in a view .My code is opening the same document for all the links which is wrong.
In my contoller :
public ActionResult Docs()
{
var docModel = this._documentBuilder.Build(this.StateData);
foreach (var doc in docModel.OldEstimateFiles)
{
return this.File(doc.PdfUrl, "application/pdf");
}
return null;
}
and in the view :
foreach (var menuItem in Model.OldEstimateFiles)
{
<ul >
<li>
#using (Html.Anchor(new ststyle { URL = "/DocumentEstimate/Docs", Target = "_blank", Text = menuItem.Label }))
{
}
</li>
</ul>
}
what is wrong in my code knowing that oldestimatefiles is a list
I edited my action in the controller to take in the ID.
public ActionResult Docs(string id)
{
var docModel = this._documentBuilder.Build(this.StateData);
return docModel.OldEstimateFiles.Any() ? this.File(docModel.OldEstimateFiles.Find(p => p.ID == id).PdfUrl, "application/pdf") : null;
}
I added an entry in the routeConfig file taking in the URL the id of the document and In the view I edited my link :
#using (Html.Anchor(new ststyle
{
URL = "/DocumentEstimate/EstimateDocs/" + menuItem.ID,
Id = menuItem.ID
It resolved the issue.

Kendo Treeview Binding parent and children

I'm trying to follow this example here
http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/treeview/ajax-binding
However, whenever I try to modify their code, I get an error saying
Error 2 'Kendo.Mvc.UI.Fluent.ReadOnlyDataSourceBuilder' does not contain a definition for 'Model' and no extension method 'Model' accepting a first argument of type 'Kendo.Mvc.UI.Fluent.ReadOnlyDataSourceBuilder' could be found (are you missing a using directive or an assembly reference?) c:\Users\Michael\Google Drive\Work\Companies\Clickable Community\dhvs\Clickable Community\Development\Portal\ClickableCommunity.Web\Views\Shared_Layout.cshtml 34 ClickableCommunity.Web
Here is my code
#(Html.Kendo().TreeView()
.Name("treeview")
// The property that specifies the text of the node
.DataTextField("Name")
.DataSource(dataSource => dataSource
.Model(model => model
// The property that uniquely identieis a node.
// The value of this property is the argument of the action method
.Id("Id")
// the boolean property that tells whether a node has children
.HasChildren("HasChildren")
)
.Read(read => read
// The action method which will return JSON
.Action("ReadCats", "Home")
)
)
)
and what I'm doing in my controller
public JsonResult ReadCats()
{
var categories = _entityLogic.GetActiveCategories();
var jsonResult = categories.Select(cat => new
{
Id = cat.Id,
Name = cat.Name,
HasChildren = categories.Where(c => c.ParentCategory == cat.Id).Any(),
ParentId = cat.ParentCategory
}).ToList();
return Json(jsonResult, JsonRequestBehavior.AllowGet);
}
Basically, I have a table that stores and ID, name, and parentcategory (the id of the parent category) and I'm trying to bind a treeview to all my parent child nodes. Thanks in advance.
Edit
So I still cannot get this. I keep getting the same error. I can't put #model namespace because it gives me an error saying it is a namespace but used like a type. Here is my entire code. Thanks,
#model ClickableCommunity.Web.Models.Public.HomeModel
#using ClickableCommunity.Core.Models.Data
#using Kendo.Mvc.UI
#{
ViewBag.Title = "Home Page";
}
<h2>#ViewBag.Message</h2>
#(Html.Kendo().TreeView()
.Name("treeview")
// The property that specifies the text of the node
.DataTextField("Name")
.DataSource(dataSource => dataSource
.Model(model => model
// The property that uniquely identieis a node.
// The value of this property is the argument of the action method
.Id("Id")
// the boolean property that tells whether a node has children
.HasChildren("HasChildren")
)
.Read(read => read
// The action method which will return JSON
.Action("ReadCats", "Home")
)
)
)
<ul>
#foreach (var item in Model.CategoryEntities)
{
<li>
#item.Name
</li>
}
</ul>
And here is my controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ClickableCommunity.Core.Contracts.Logging;
using ClickableCommunity.Core.Contracts.Logic;
using ClickableCommunity.Web.Models.Public;
namespace ClickableCommunity.Web.Controllers
{
public class HomeController : BaseController
{
private readonly IEntityLogic _entityLogic;
private readonly IGeogrpaphyLogic _geoLogic;
private readonly IUserLogic _userLogic;
public HomeController(ISystemLogger logger, IEntityLogic entityLogic, IUserLogic userLogic, IGeogrpaphyLogic geoLogic) : base(logger)
{
_entityLogic = entityLogic;
_userLogic = userLogic;
_geoLogic = geoLogic;
}
public ActionResult Index()
{
var catEnts = new List<HomeModel.CategoryEntitiesList>();
var model = new HomeModel
{
AvailableCategories = _entityLogic.GetActiveCategories()
, Entities = _entityLogic.GetActiveEntities()
, States = _geoLogic.GetAllStates()
};
var tempCe = new HomeModel.CategoryEntitiesList();
foreach (var i in model.AvailableCategories.Where(c => c.ParentCategory == null))
{
tempCe = new HomeModel.CategoryEntitiesList();
tempCe.Name = i.Name;
tempCe.ParentCategory = i.ParentCategory;
tempCe.Id = i.Id;
tempCe.HasChildren = model.AvailableCategories.Where(a => a.ParentCategory == i.Id).Any();
catEnts.Add(tempCe);
}
model.CategoryEntities = catEnts;
return View(model);
//return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public JsonResult GetChildTreeViewData(int? id)
{
var categories = _entityLogic.GetActiveCategories();
if (id != null)
{
categories = categories.Where(c => c.ParentCategory == id);
}
var jsonResult = categories.Select(cat => new
{
Id = cat.Id,
Name = cat.Name,
HasChildren = categories.Where(c => c.ParentCategory == cat.Id).Any(),
ParentCategory = cat.ParentCategory
}).ToList();
return Json(jsonResult, JsonRequestBehavior.AllowGet);
}
}
}
Your View should define a Model in order for this to work
Add this line to the top of your View:
#model categories //or whatever namespace your Entity categoies are in
this should work fine!!

MVC with Kendo treeview - remote data JSON or XML

I'm trying to create a Kendo treeview that gets populated from my controller (PropertyController).
The part I'm stuck at is how to format my data in the controller. How could I create a treeview three items deep and pass that to my view to be displayed in the treeview?
#(Html.Kendo().TreeView()
.Name("treeview")
.Events(events => events
.DragStart("PartnershipPage.OnDragStart")
.Drop("PartnershipPage.OnDrop")
.DragEnd("PartnershipPage.OnDragEnd")
)
.HighlightPath(true)
.DragAndDrop(true)
.DataSource(dataSource => dataSource
.Read(read => read
.Action("Index","Tree")
)
)
)
I've included the controller to see if I'm doing it correctly. All that is happening so far is the JSON is being displayed on the screen as text.
Controller:
public ActionResult Index()
{
var org = new Entities();
var eList = new List<Entity>();
var entity1 = new Entity
{
Id = 1,
Name = "LLC-A",
parentId = 0
};
eList.Add(entity1);
var entity2 = new Entity
{
Id = 2,
Name = "LLC-B",
parentId = 0
};
eList.Add(entity2);
var entity3 = new Entity
{
Id = 1,
Name = "LLC-C",
parentId = 2
};
eList.Add(entity3);
var entity4 = new Entity
{
Id = 1,
Name = "LLC-D",
parentId = 2
};
eList.Add(entity4);
org.Entity = eList;
var test = from x in org.Entity
where (x.Name != null)
select new
{
Id = x.Id,
Name = x.Name,
parentId = x.parentId
};
;
return Json(test, JsonRequestBehavior.AllowGet);
}
From: Kendo Site
<%= Html.Kendo().TreeView()
.Name("TreeView")
.BindTo(Model, mapping => mapping
.For<Customer>(binding => binding
.Children(c => c.Orders) // The "child" items will be bound to the the "Orders" property
.ItemDataBound((item, c) => item.Text = c.ContactName) // Map "Customer" properties to TreeViewItem properties
)
.For<Order<(binding => binding
.Children(o => null) // "Orders" do not have child objects so return "null"
.ItemDataBound((item, o) => item.Text = o.OrderID.ToString()) // Map "Order" properties to TreeViewItem properties
)
)
%>
You could put your entities into a list called for example, "myEntities" and return that from the controller to the view:
public ActionResult Index()
{
var ents = getMyEntities(); // some method you have to return the list of your entities
return ents;
}
Then in your view, you can loop through all the Entities in your Model:
#(Html.Kendo().TreeView()
.Name("TreeView")
.Items(treeview =>
{
foreach (var entity in Model)
{
var entityName = entity.Name;
var children = entity.Children;
treeview.Add().Text(entityName ).Expanded(false).Items(branch =>
{
if (children != null)
{
foreach (var child in children)
{
branch.Add().Text(child);
}
}
});
}
}
)
)
I used children because I found it easier to use than parent and so I would change my Entities to something like this:
var entity4 = new Entity
{
Id = 1,
Name = "LLC-D",
Children = <list of children names ... >
};
You can see how I did mine here: Populate KendoUI Treeview with RavenDB Documents
Hope this helps.
Edit in response to: How can I display children of children?
I ran into the same problem as you describe (displaying children of children). Here is how I did it after I had the problem of displaying children of children:
I used EntityFramework:
The DB context class:
public class EntityDBContext : DbContext
{
public DbSet<MyEntity> Entities { get; set; }
}
Controller:
public JsonResult EntitiesForTreeView(int? id)
{
// Here I am using EntityFramework
var entitiesContext = new EntityDBContext();
var myEntity= from e in entitiesContext.Entities
where (id.HasValue ? e.Parent == id : e.Parent == null)
select new
{
id = e.Id,
Name = e.Name,
hasChildren = e.Id
};
return Json(myEntity, JsonRequestBehavior.AllowGet);
}
And the View:
#(Html.Kendo().TreeView()
.Name("treeview")
.DataTextField("Name")
.LoadOnDemand(true)
.HighlightPath(true)
.DataSource(dataSource => dataSource
.Read(read => read
.Action("EntitiesForTreeView", "SiteMap")
)
)
.Checkboxes(true)
)
Hope this helps. Sorry if there is something stupid - I've only been doing web development for 3 months so unmark as answer if needed. Let me know if I can help further. PS: I should mention that after implementing the solution I have shown above, I noticed that the nodes on my tree all have the "+" sign... regardless of whether or not they have any children. If you know how to fix this, please let me know :)

Problem with using a ToExpando method

Hi I i try to use a ToExpando solution to use anonymous classes in razor views.
I use this solution -> Dynamic Anonymous type in Razor causes RuntimeBinderException
I'll write what i did:
I added a file Extensions.cs where i put following code:
public static class Extensions
{
public static ExpandoObject ToExpando(this object anonymousObject)
{
IDictionary<string, object> anonymousDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(anonymousObject);
IDictionary<string, object> expando = new ExpandoObject();
foreach (var item in anonymousDictionary)
expando.Add(item);
return (ExpandoObject)expando;
}
}
I wrote a query that receive a tuples from database in controller method:
IEnumerable<dynamic> articles = (from p in db.Articles.Where(p => p.user_id == 2)
select new
{
p.article_id,
p.title,
p.date,
p.category,
AverageScore = db.Articles_Scores
.Where(o => o.user_id == p.user_id && p.article_id == o.article_id)
.Average(m => m.score)
}).AsEnumerable()
.Select(r => r.ToExpando());
int ii = 0;
foreach(var it in articles) {
// HERE I CAN READ EVERYTHING
ii = it.article_id;
}
return View(articles);
In view I declare a model:
#model IEnumerable<dynamic>
And I try to get every tuples:
#foreach (dynamic item in Model) {
// some code
#item.article_id // HERE IS EXCEPTION
}
In the foreach line I got an Exception:
RuntimeBinderException: 'System.Dynamic.ExpandoObject' does not contain a definition for 'article_id'
What did I do wrong?
You need to call .AsEnumerable() or .ToList() first to force ToExpando to run on the client.
Try:
dynamic articles = (from p in db.Articles.Where(p => p.user_id == 2_
select new
{
p.article_id,
p.title,
p.date,
p.category,
AverageScore = db.Articles_Scores
.Where(o => o.user_id == p.user_id && p.article_id == o.article_id)
.Average(m => m.score)
}).AsEnumerable()
.Select(r => r.ToExpando());
Edit: Make sure you declare dynamic not var
Edit 2: In your for look, you're declaring var again. Change it to:
#foreach (dynamic item in Model) {
// some code
#item.article_id // HERE IS EXCEPTION
}
Ok, building on the first two answers, I added another extension method that I'm surprised I don't see here:
public static List<ExpandoObject> ToExpandoList<T>(this IEnumerable<T> ie) {
return ie.Select(o => o.ToExpando()).ToList();
}
And now in my view I have code like this, and it works just fine using Razor.
var peoples = from f in "tom dick susan roberto".Split(' ')
select new { FirstName = f, Age = f.Length };
ViewBag.People = peoples.ToExpandoList();
The problem is HtmlHelper.AnonymousObjectToHtmlAttributes(), it's replacing
'_' with '-' for the property name. Look at the comment for the method.

Resources