Server side data table in asp.net mvc - asp.net-mvc

I have a server side Data Table without any attribute searching or sorting. And now I want to add these attribute.
According to the searches I have, I have to define variables in my controller like this:
public int Start = Convert.ToInt32(Request["start"]);
But my controller does not understand the word Request and gives an error
How can I resolve this error?

Have you included the System.Web assembly in the application?
using System.Web;
If not, try specifying the System.Web namespace, for example:
public int Start = Convert.ToInt32(System.Web.HttpContext.Request["start"]);

Related

How to use a shared resource for localization in .net core 2 in controllers?

I'm new to .net core development after years of working in .net framework on mostly webform applications.
I'm trying to localize a new project and looking at options determined for this particular use that a shared resource would be the most maintainable solution long term and followed this example: https://damienbod.com/2017/11/01/shared-localization-in-asp-net-core-mvc/
This appears to work great for adding the localized data in the view but I am struggling to be able to do so in the controller such as returning a localized error when something is caught server side and a custom message would be returned.
In my controller's I added
private readonly LocService _SharedLocalizer;
Within a view's method in the controller if I try and add
ViewBag.localizedmessage = _SharedLocalizer.GetLocalizedHtmlString("message")
I get a null error on accessing the page on this line.
If I create a new instance within the view's method I am not sure what to provide as the argument value for the IStringLocalizerFactory.
_SharedLocalizer = new LocService();
What is the piece I am missing or how do I go about properly accessing the shared resource in a controller?
Try this
public class YourController : Controller
{
private readonly LocService _SharedLocalizer;
public YourController(LocService localizer)
{
_SharedLocalizer = localizer;
}
}

Conditional namespaces in mvc views

I'm working on MVC 3 and using resource files to localize the application. Now we have another customer on board for the application and they would like to change some of the text on the application..typical.
I have create a separated resource file for them and would like to do something like this in views
if (customer =A )
#using Resources.customerA
else
#using Resources.customerB
I've a resource class in both namespaces so something like this works fine if I change the namespace
Resource.WelcomeUser
Is it possible to use conditional using statement in views? I'm unable to find the right syntax for this. Any ideas?
You can put both using statements in View, but when you use classes you would have to write some namespace prefix.
Example:
#using Project.Resources.customerA
#using Project.Resources.customerB
using classes:
customerA.WelcomeUser
customerB.WelcomeUser
I think there is no other way, because two files cannot have the same path.
What you're really talking about is the provider pattern. You have two (or more) interchangeable things, and you want to be able to use one or the other contextually.
The correct way to do this in an OO context is to create and use an interface, while then injecting the actual implementation you want at runtime. You can actually achieve this in ASP.NET Core, which supports injection in Views, but in ASP.NET MVC 5 and previous, you'd need to go a little out of your way. I'm imagining these are currently static classes, since you're referencing them merely via namespace. With that approach, you'd need to follow #Ssheverdin's advice and use the FQN of the class (i.e. with the namespace):
#if (customer == A)
{
#Resources.customerA.StaticClass.Property
}
else
{
#Resources.customerB.StaticClass.Property
}
Alternatively, you could change the static classes to be instance classes and use a factory pattern to return the right one. This is a very simplistic example, but hopefully enough to convey the idea:
public static class ResourceFactory
{
public static IResourceClass GetForCustomer(string customer)
{
switch (customer)
{
case "A":
return new Resources.customerA.ResourceClass();
default:
return new Resources.customerB.ResourceClass();
}
}
Then:
#{ var resource = ResourceFactory.GetForCustomer(customer); }
I have managed to achieve the behaviour by adding a web.config file under views folder and including the namespaces there, i have to remove the #using statement from all views obviously. You might find that intellisense doesn't work anymore for you so try closing all views and reopen them again.
With this way I can create a separate web.config file for each customer and specify the relevant namespaces accordingly. Now just have to make sure to provide the RIGHT config file for each customer when deploying the release:)

Error:'Unsupported context type' while creating a new controller

I am going to implement MvcMusicStore using ASP.NET MVC3, Linq to Sql class instead of Entity Framework, MS SQL Server 2008 pro instead of express ed.
I got the tutorial from mvcmusicstore.codeplex.com
I used Linq to Sql class and the Datacontext is MvcMusicSrotedataContext. When i try to create a new class using this
it shows an error in a new window when i click add button Error:'Unsupported context Type'
So, could you please help me to solve this?
Thank You.
The built-in MVC scaffolding doesn't support Linq to SQL -- you'll have to use Entity Framework instead. (Or don't use the scaffolding, build your own controller/action logic manually. Or use a scaffolding plugin that supports Linq to SQL.)
I got the same issue with EF. I am using the VS 2012.
Background:
The reason for my case was.. this auto generation process (Scaffolding) seems not recognize the partial class concept.
I used the model first approach and I have used inheritance with the entities.
Ex: entity “B” and “C” is inherited from “A”
So in my generated model class “DataModelContainer” which is inherited from “DbContext”,
There is no definition for “DbSet” and “DbSet”
i.e: the following two lines were not there
public DbSet<B> B { get; set; }
public DbSet<C> C { get; set; }
Generated “DataModelContainer” class I a partial class, so I completed the other part, using the concept of partial class. And this would be a problem for Scaffolding.
Solution
My workaround was, just removed the partial class I added manually. And added the definitions for “DbSet” and “DbSet” in to the auto generated class.
The problem with this solution is, I have to repeat the same thing when I regenerate the model classes.

Returning a list of categories nopCommerce

I am trying to return 2 lists using nopCommerce, Both lists are read-only,
I am looking for the simplest way in doing this as I am writing a metro app and I don't really want to spend weeks learning MVC .
The first list is a list of categories from the base nopCommerce platform , the second list is a list of products.
Both lists need to be returned as JSON, to the calling client.
I have two questions:
Is there a way I can get this list without calling custom code ?
I wrote a plugin using the following code
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Services.Catalog;
using Nop.Services.Customers;
using Nop.Core.Plugins;
namespace Nop.Plugin.Other.ONWWW.Controllers
{
public class ONWWWController : Controller
{
public ICategoryService _categoryService;
public ONWWWController(ICategoryService categoryService)
{
this._categoryService = categoryService;
}
public ActionResult Index()
{
return Json(_categoryService.GetAllCategories(), JsonRequestBehavior.AllowGet);
}
}
}
Why, when i run the code, I get the following error ?
No parameterless constructor defined for this object.
Question 1: Your best bet is the Nop.Plugin.Misc.WebServices plugin in the original source code.
Question 2: Did you see that the only constructor you have is
public ONWWWController(ICategoryService categoryService)
Which must accept a parameter? In other words, you have not registered the dependency properly. Try looking at one of the DependencyRegistrar.cs file in any of the default plugin. The Nop.Plugin.Misc.MailChimp plugin, for example, has a DependencyRegistrar.cs that you can refer.
:)

Load Balancing using SQL Server and TempData MVC

I've been told that MVC 1.0 TempData does not work under a load balancer when using SQL Server and that it is because the Dictionary itself is not serializable.
We require this for a project and are looking to be able load balancer effectively.
So I would be very grateful if someone could answer the following questions:
Is there away around this so you can make it work?
Is this fixed in MVC 2.0?
Can we create a ITempDataProvider to fix it?
Or has anyone made a fix to the source code for a project of their own they would like to share?
Cheers,
Jamie
The dictionary itself doesn't need to be serializable. It is what you store inside TempData that needs to be serializable. So for example if you have the following class
[Serializable]
public class Foo
{
public string Bar { get; set; }
}
You can perfectly fine use SQL server for session persistence and write the following code:
TempData["foo"] = new Foo { Bar = "bar" };
Session["foo"] = new Foo { Bar = "bar" };
Mmmm, so any UI model (ASP.Net MVC) would just require the Serializable attribute and that should just work?
How does it work for lists and collection based UI models?

Resources