Missing Namespace in ASP.NET MVC & Entity Framework - c#

I am just trying to learn about creating web applications using ASP.NET MVC and I'm following some tutorial I have found online, but they are not giving any answers and I'm not sure why I am getting an error as I am following it.
The error I get is:
The type or namespace (Genre) could not be found.
I have created a SQL database and also 2 models and a controller, no view so far as it isn't required. The models are named ForestContext and the other is named Music.
Music will be where the database is linked to.
If I open up the music class, there is namespace genre defined as a string.
The code I am using to display the genres of music is;
public ActionResult Index()
{
IList<Genre> genrelist = _context.Genre.ToList();
return View(genrelist);
}
I am not sure why it doesn't recognize that I am using the namespace Genre. It also states the error of not recognizing _context but I assume this is from the same class (Music).
I have also added;
using MyFirstForest.Models
As before this, I was asked to create an object of the ForestContext class, done so as;
private ForestContext context;
public HomeController()
{
context = new ForestContext();
}
And that wouldn't work unless I added using MyFirstForest.Models.
Please let me know if I need to post any more information but this is all I have got to so far.
Any help would be hugely appreciated.
Regards,
DC

Related

ASP.NET Core View

I am not sure how to get my SQL view to actually be read from the .cshtml file. I have my model created to relate to the view and have added the DbSet to the appdbcontext class. I have also done the keyless on the onModelBuilder and created this
public async Task<IActionResult> PreviewEmailAsync()
{
var myTableResults = _context.WetWeatherReportViews.FromSqlRaw(
"Select * from dbo.WetWeatherReportView").ToList();
ViewBag.Data = myTableResults;
return View();
}
Not sure how to get the view to showcase the information within, I have tried multiple different methods and believe the ViewBag is my closest one, but don't believe I am doing it right. I am unable to get it to work and not sure what I am missing. Thank you for the help!

'IdentityContext' could not be found (are you missing a using directive or an assembly reference)

I am migrate the project https://github.com/attilah/AngularJSAuthentication to using mongodb driver verson 2 , and I am in the final step here:
namespace AngularJSAuthentication.API
{
using AspNet.Identity.MongoDB;
using Entities;
using MongoDB.Driver;
public class ApplicationIdentityContext : IdentityContext
{
public ApplicationIdentityContext(IMongoContext mongoContext)
: this(mongoContext.Users, mongoContext.Roles)
{
}
public ApplicationIdentityContext(IMongoCollection<User> users, IMongoCollection<Role> roles)
: base(users, roles)
{
}
}
}
Then I get this error
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'IdentityContext' could not be found (are you missing a using directive or an assembly reference?) AngularJSAuthentication.API D:\Projects\AngularJSAuthentication-master\AngularJSAuthentication.API\ApplicationIdentityContext.cs
Here is my current source code
https://github.com/skanel/Angular2-WebApi2-Mongodb2-Authentication
UPDATED
#Swagata Prateek, thank for your code,however when I run the It stop working in direct me to this block of code in startup.cs
private void ConfigureOAuth(IAppBuilder app, IContainer container)
{
var OAuthServerOptions = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = container.Resolve<IOAuthAuthorizationServerProvider>(),
RefreshTokenProvider = container.Resolve<IAuthenticationTokenProvider>()
};
Error
An exception of type 'Autofac.Core.DependencyResolutionException'
occurred in Autofac.dll but was not handled in user code
Additional information: An error occurred during the activation of a
particular registration. See the inner exception for details.
Registration: Activator = SimpleAuthorizationServerProvider
(ReflectionActivator), Services =
[Microsoft.Owin.Security.OAuth.IOAuthAuthorizationServerProvider],
Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared,
Ownership = OwnedByLifetimeScope
I personally used the same example for my Asp.net Identity implementation over Mongodb and the reason you're missing that class is because of the fact the sample is pretty old and the Identity extension for mongodb it depends on is here and it has progressed to updated versions already.
I can't go with the full description here on how I used it but I surely can point you to my open source project here where I've learned form the example that you mentioned. I hope it would solve your problem on implementing Asp.net Identity with Mongodb.
Update
If you want to have the exactly same solution that Attila Hajdrik wrote in the git repo please make sure you have exactly the same package version the AspNet.Identity.MongoDB defined here. Because the library itself is now upgraded itself and I presume you either updated all the nuget packages or recreated the whole solution written in the github repo according to your need. In both cases, you might end up with a version of AspNet.Identity.MongoDB you don't want to use. This one should be the shortest and easiest solution for your need.
Now on my solution mentioned above from my github repo. I used my own IAccountContext and I used UserManager<User> as my base AccountManager and UserStore<User> as my underlying store for the manager. Here User class is the Identity class that Im using which is derived from IdentityUser.
Technically you can build your own context out easily and you don't
really have to rely on the given example to the fullest.
My sample AccountContext would be :
public class AccountContext : IAccountContext
{
private readonly IDbContext dbContext;
private readonly AccountManager accountManager;
public AccountContext(
IDbContext dbContext,
AccountManager accoutnManager)
{
this.dbContext = dbContext;
this.accountManager = accoutnManager;
}
// Your own stuff here
}
Here, AccountManager is a UserManager<T> derivative and it takes a IUserStore<User> in it's constructor. It practically gives you more freedom on how you want to design your Identity layer. :)
Hope this helps.
A lot of this package versions are old and can be an issue be compatibility. If nothing is preventing you from updating, please do update the packages to latest versions.
When you do update, just FYI that IdentityContext does not exists anymore in the AspNet.Identity.MongoDb package/library. See source code in Github: https://github.com/g0t4/aspnet-identity-mongo

Upgraded from Umbraco v4 to v7 SurfaceController No route in the route table matches the supplied values

I recently converted our intranet Umbraco site from v4 to v7.2 and also converted all the webform masterpages to mvc.
I am trying to convert a usercontrol that should be a child action to a SurfaceController but I am getting the dreaded "No route in the route table matches the supplied values" error when trying to call the action:
#Html.Action("ServiceStatusInfo", "ServiceStatusSurface")
This is just a get action that doesn't require a view or a model. It just calls the action on the server and the server updates a file on the server that then get's read by some javascript.
I have done a lot of searching and I created a sample solution using Umbraco 7 and created a controllers folder, then a "MySurfaceController" and I was able to call the action from the masterpage of the sample solution with no issues but in the recently converted project it seems like there is some weird routing issue going on. I compared the web.config's for both the current project and the sample one and they pretty much have the same entries (I thought maybe I missed something). It seems that my converted project is not recognizing the routing. Any help will be appreciated.
Here is the SurfaceController
using Umbraco.Web.Mvc;
using System.Web.Mvc;
namespace MyUmbracoApp.Controllers
{
public class ServiceStatusSurfaceController : SurfaceController
{
// can't reach this either:
public ActionResult Index()
{
return Content("hello world");
}
// this is what I am trying to reach
[ChildActionOnly]
public ActionResult ServiceStatusInfo()
{
// do some stuff to get the status
return CurrentUmbracoPage();
}
}
}
I have also tried using the "PluginController" option even though this is not a plugin with the "area" attribute but same problem.
Maybe there is a workaround that I am not aware of ?
Change StatusInfo to ServiceStatusInfo in your action call. This should match the name of the action.
#Html.Action("ServiceStatusInfo", "ServiceStatusSurface")

Sitefinity MVC widgets not registering

Hopefully someone can help me with this, because Sitefinity support appears to be stumped and the community on the G+ page seems stumped also.
I have a dead simple MVC widget:
Controller [~/Mvc/Controllers/TestWidgetController.cs]:
using System.Web.Mvc;
using Telerik.Sitefinity.Mvc;
namespace SitefinityWebApp.Mvc.Controllers
{
[ControllerToolboxItem(Name="TestWidget", Title="Test widget", SectionName="Test section")]
public class TestWidgetController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Model [~/Mvc/Models/TestWidgetModel.cs]:
namespace SitefinityWebApp.Mvc.Models
{
public class TestWidgetModel
{
}
}
View [~/Mvc/Views/TestWidget/Index.cshtml]:
<p>Test</p>
If I take the controller, the model and the view and drop them into a new Sitefinity project, and then build that project, the widget gets registered automatically without any problems and I can use it right away.
However, I have one specific Sitefinity project that I want to drop this widget into, and that project when built does not register the widget automatically. If I try to register the widget manually I get NullReference exceptions. It's like the project doesn't actually see those files somehow.
Does anyone have any idea what could be going on here? I think the simplicity of the widget and the fact that it works in a new Sitefinity project indicates that it's definitely something wrong with the way this particular project has been configured, but I can't figure out what the problem is.
For what it's worth here's the Solution Explorer for the problematic project:
You need to tell that the controller the page which is returned. Based on the file directory, it is Index.cshtml Therefore:
using System.Web.Mvc;
using Telerik.Sitefinity.Mvc;
namespace SitefinityWebApp.Mvc.Controllers
{
[ControllerToolboxItem(Name="TestWidget", Title="Test widget", SectionName="Test section")]
public class TestWidgetController : Controller
{
public ActionResult Index()
{
return View("Index");
}
}
}
It is now possible to have MVC widget in a separate assembly that automatically registers itself to the toolbox.
Check this doc for more info
Could you try to rename the controller and it toolbox name? Sometime Sitefinity works funny
It seems that you are having an issue regarding the widget registration in the controller store. Since most probably this issue is caused by problems related to this specific project it is highly advisable that you go and open a support ticket to the Sitefinity support system. By doing so your project can be inspected in more details and the most appropriate actions will be recommended.
Apart from that would you please point out the version of your SItefinity project? It will definitely help in the problem investigation.
Also there is an error log file which can be found at ~\App_Data\Sitefinity\Logs. Check and see what is logged there. This can give a clue about what is causing the issue and where to look at for possible solutions.
Regards,
Desislav

Kendo UI Scheduler connection to database

I'm currently developing a web app using the open source Kendo UI Scheduler with ASP.NET MVC Visual Studio 2012. But i'm experiencing some trouble while trying to connect my scheduler with a local database to store the bookings made by users of my application.
I've been looking for documentation to help me with this but I haven't been able to set this up entirely...
I followed instructions about this on: http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/scheduler/ajax-editing
I've added the Entity Framework Data Model and necessary code in Models like TaskViewModel but the problem occurs in my Controllers.
public ActionResult Tasks_Read([DataSourceRequest]DataSourceRequest request)
{
using (var sampleDB = new SchedulerEntities())
{
IQueryable<TaskViewModel> tasks = sampleDB.Tasks.ToList().Select(task => new TaskViewModel()
{
TaskID = task.TaskID,
Title = task.Title,
//Specify the DateTimeKind to be UTC
Start = Convert.ToDateTime(task.Start),
End = Convert.ToDateTime(task.End),
Description = task.Deschription,
}).AsQueryable();
return Json(tasks.ToDataSourceResult(request));
}
}
I get an error on the DataSourceRequest: the type or namespace DataSourceRequest could not be found. But I can't find to narrow down which module I'm missing or what else I'm doing wrong...
Besides that I also get the following error System.LinqIQueryable doest not contain a definition for ToDataSourceResult. on the code:
return Json(tasks.ToDataSourceResult(request));
Anyone who can help me here or has an other/better solution to make a connection to a local database using the open source Kendo UI Scheduler?
Any help would be really appreciated!
You need to specify the allow get behavior.
return Json(tasks.ToDataSourceResult(request), JsonRequestBehavior.AllowGet));

Categories

Resources