Kendo UI Scheduler connection to database - c#

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));

Related

Missing Namespace in ASP.NET MVC & Entity Framework

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

ASP.NET Core tutorial error 'The value 'http://msdn.com' is not valid for Url

I'm running the app in this Microsoft Tutorial and when I try to enter a url in the input box of the app (for instance, http://msdn.com or http://blogs.msdn.com/adonet as shown in the tutorial) and click submit I get the following validation error:
The value 'http://msdn.com' is not valid for Url.
When I debug the application I notice that in the following code of the tutorial the ModelState.IsValid value is showing as 'false'. What is missing here and how can it be fixed?
public IActionResult Create(Blog blog)
{
if(ModelState.IsValid)
{
_context.Blogs.Add(blog);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(blog);
}
Points to note:
I'm using latest ASP.NET Core 1.0 and VS2015-Update 3 [released on June 27, 2016] on windows 8.1
In the Creat.chtml view of the tutorial, <input asp-for="Url" class="form-control" /> was initially showing as readonly so I added type="text" attribute there and it became read/write
My app is an exact copy of the tutorial (I did copy/past) except that instead of choosing 'No Authentication' I chose 'Individual User Accounts' option when creating the poroject. But that, I think, should not make any difference.
UPDATE
I added var errors = ModelState.Values.SelectMany(v => v.Errors); just above if(ModelState.IsValid) statement and I see the following in the debug window showing the values of error collection but can't figure out the cause of error. Maybe. someone can help:
UPDATE 2:
I found my mistake. I was using int datatype in public int Url { get; set; } property of the Blog class instead of using string. Changed it to string and it's working now. Someone may help what the error in image is saying.
I resolved the issue. There was a mistake on my part. I was using the datatype int in the property public int Url { get; set; } of the Blog class. Changed it to string. Then deleted the database in SQL Server and corresponding migration folder in the project. Re-ran the package manager commands Add-Migration MyFirstMigration -context BloggingContext and Update-Database -context BloggingContext. It's working now. Although the app was a copy/paste from this Microsoft Tutorial, later I tried to test creating a property through a short-cut key as explained here but forgot that the short-cut key creates the datatype as int. Thanks to all the readers who may have tried to help.

SQL write to ASP.NET user table doesn't save

My setup:
ASP.NET 4.5 web api (on Azure) saving data to SQL db (also on Azure)
AngularJS web front end (another Azure web site)
When a user first signs up, I show them a "getting started intro". The intro is only supposed to run once - I log the timestamp of the intro launch date as a custom field in the ASP.NET user table.
Imagine my surprise when I log in (as a user would) and see the intro TWICE.
The AngularJS front end is properly sending the "intro viewed" message to the ASP.NET api, and the api responds with a success message. However, when I look at the raw data in the db, the timestamp is most definitely NOT updated. Consequently, the user will see the intro a second time (at which point the timestamp gets recorded in the db properly).
I have a crappy workaround. After the client requests an OAuth Bearer token from my server, the client then requests user information (to decide whether or not to show the tour). Waiting 100ms and then sending the "tour viewed" message back to the server masks the issue.
I've not seen ANY other issues storing data at any point. Because our db is on Azure, I can't hook up Profiler and the built in auditing doesn't give me any clues.
Is there something about requesting the token that leaves ASP.NET identity in a funny state? And it takes a brief wait before you can write to the table? Are custom fields that extend the base Identity setup prone to problems like this? Is the UserManager possibly doing something weird in its black box?
Does anyone have suggestions for how to continue debugging this problem? Or ever hear of anything like it?
Here's the relevant code that should be updating the "tour viewed" timestamp in the db:
[HttpPost, Route("UserInfo")]
public async Task<IHttpActionResult> UpdateUserInfo(UpdateBindingModel model)
{
var currentUser = UserManager.FindById(User.Identity.GetUserId());
if (model.FirstName != null)
{
currentUser.FirstName = model.FirstName;
}
if (model.LastName != null)
{
currentUser.LastName = model.LastName;
}
if (model.SetIntroViewCompleteDate)
{
currentUser.IntroViewCompleteDate = DateTime.UtcNow;
}
if (model.SetIntroViewLaunchDate)
{
currentUser.IntroViewLaunchDate = DateTime.UtcNow;
}
if (model.SetTipTourCompleteDate)
{
currentUser.TipTourCompleteDate = DateTime.UtcNow;
}
if (model.SetTipTourLaunchDate)
{
currentUser.TipTourLaunchDate = DateTime.UtcNow;
}
IdentityResult result = await UserManager.UpdateAsync(currentUser);
if (result.Succeeded)
{
var data = new UserInfoViewModel
{
FirstName = currentUser.FirstName,
LastName = currentUser.LastName,
IntroViewLaunchDate = currentUser.IntroViewLaunchDate
};
return Ok(data);
}
return InternalServerError();
}
UPDATE ********* 4/18
I've also tried to move completely away from UserManager stuff. I've tried the following modifications (pulling the user data from a table like I would access any other data), but it still behaves the same. I'm starting to think that putting custom fields on the ApplicationUser object is a bad idea...
New db retrieve and save looks like this:
ApplicationDbContext newContext = new ApplicationDbContext();
var currentUser = await (from c in newContext.Users
where c.Email == User.Identity.Name
select c).SingleOrDefaultAsync();
//update some values
await newContext.SaveChangesAsync();
Basically the problem might be with initialization of the `UserManager' and the fact that this class works on the db context so you need to persist changes to that context. Here is an example:
var userStore = new UserStore<ApplicationUser>(new MyDbContext());
var userManager = new UserManager(userStore);
That way you remember both manager and context. Then in your method you would normally call:
IdentityResult result = await userManager.UpdateAsync(currentUser);
followed by persisting this change to db context:
var dbContext = userStore.context;
dbContext.saveChanges();
Based on your comment that waiting 100ms masks the issue, I think you may have a problem with the multiple async await calls. Try running the calls synchronously and see if you still have the same issue. My guess is that the problem might go away. My experience has been that using async await can be tricky when you have calls to asynchronous methods that call other asynchronous methods. You may have code that is executing without the proper results returned.
Well, here's what I did to solve the problem. I totally de-coupled my custom user data from the built in ASP.NET identity stuff. I've now got a separate object (and therefore separate SQL table) that stores things like FirstName, LastName, LastActiveDate, etc, etc.
This has solved my problem entirely, though it has introduced another call to the database in certain situations. I've deemed it to be not a big enough performance issue to worry about. I'm left thinking that this was some sort of weird race condition involving the generation of a token for an ASP.NET identity user then quickly writing to an Azure SQL database - lord knows what it was exactly in my code that caused the problem.
If you've got a problem that's hard to solve, often the best plan is to change the problem.
Now I need to find a meta thread discussing what to do with bounty points when you've blown up the problem...

How do I connect a cshtml to a offsite database server?

I am working on a group project using MVC and I am trying to get the Orders view to connect to our database which is on an offsite server.
The few tutorials I have seen concerning incorporating a database with a ASP.NET MVC project talks about adding the code
var db = Database.Open("nameofdatabases");
My problem is that when I start to type Database, the little drop down menu showing what options are available does not show it, DataBindings is the closest it gets.
Also, someone had showed us another line of code:
public VivaceContext db = new VivaceContext();
List<Distributor> list = new List<Distributor>();
public ActionResult Index()
{
var ist = db.TableName.FirstOrDefault(x => x.FieldName == "").Orders.ToList();
var i = db.TableName.Where(x => x.FieldName == "").ToList();
return View();
}
What I am needing to do is retrieve information from several tables in a database at a different location, add data, edit data, and delete data.
You Can do this Using Model.
Create a Context Class For Each and Every Database which you want to use.
Then Create Strongly Type Controller For your Table it will allowed to do all CRUD operation on that table.

Xamarin trying to get users from webservice, immediate crash

Hi again stackoverflow,
I am following a tutorial on how to build an Android application in Xamarin and I have encountered an error I cannot resolve on my own.
Hoping anyone of you might shed some light on how to proceed from here:
This code is copied from the tutorial itself (source: pluralsight)
private JsonServiceClient client;
private IList<User> users;
void PopulateSelectUsers ()
{
var response = client.Get(new Users());
users = response.Users.ToList ();
var names = users.Select (u => u.Name);
var usersSpinner = FindViewById<Spinner> (Resource.Id.usersSpinner);
usersSpinner.Adapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleListItem1, names.ToArray ());
}
Where "Users" is a request:
public object Get(Users request)
{
return new UsersResponse { Users = Repository.GetUsers() };
}
[Route("/users", "GET")]
public class Users : IReturn<UsersResponse>
{
}
public class UsersResponse
{
public IEnumerable<User> Users { get; set; }
}
HOWEVER once Xamarin read this line of code:
var response = client.Get(new Users());
then the application in the emulator just crashes and Xamarin leaves me no information on what happened or how to fix it..
It seems that this code works in the tutorial and as I mentioned before, Xamarin leaves me no information on what happened or how to fix it, so my question would be if perhaps one of you know what Is happening or perhaps a way to fix it.
Also perhaps worth mentioning is that I'm using redis to store users.
IF you want to view the userservice it is available here:
http://shan13alwo.cloudapp.net/api/metadata
You can check if code works by sending JSON GET to http://shan13alwo.cloudapp.net/api/users
Thank you in advance,
UPDATE:
I realize I might have been unclear of what I wanted to do but to simplify. What I want to do Is get my users(ienumerable) from my redis database and store them in a List. Using the this code in Xamarin does not work:
var response = client.Get(new Users());
users = response.Users.ToList ();
As Scott previously pointed out to me In another topic made here on Stackoverflow:
ServiceStack v4 was JUST released and using different versions of the servicestack library in Xamarin and Visual Studio resulted in this strange behavior.
Reverting back to V3 of ServiceStack solved my issues.
I would like to thank Scott for your assistance and would like to ask one last thing:
Where can I download the VERSION 3, free license version of the ServiceStack libraries for Android?
(Think its called "AndroidIndie")
Downloading and compiling the solution with the libraries from the lib folder of ServiceStack results in "evaluation software, build valid for 24 hours".
Thank you in advance.

Categories

Resources