Telerik grid export functionality, issue with ToGridModel() - asp.net-mvc

I have included Telerik.Web.Mvc assembly and still not able to get ToGridModel() method for export function.
Below are some details about my code
Html.ActionLink(" ", "ExportData", new {..})
public ActionResult ExportData(...)
{
}
issues in code
Not able to define variable with IEnumerable xxx (giving error as it required 1 argument)
Not able to call ToGridModel() method.
Can anybody guide me how should i do this? what I am missing?

Make sure you've brought the extension method into scope by adding the proper using directive pointing to the namespace in which this extension method is defined:
using Telerik.Web.Mvc.Extensions;

Related

Access MvcResources under System.Web.Mvc from custom helper method

I am working on a custom Html helper extension method with MVC 5.0, and after walking through the source code of the built-in helper method: InputHelper(), I want to use a piece of it in my helper method:
if (String.IsNullOrEmpty(fullName))
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
}
However, even with the namespace System.Web.Mvc used, I am still getting the error saying: the name 'MvcResources' is not found in the current context.
According to its source code, MvcResources is defined in a .resx file:https://github.com/ASP-NET-MVC/aspnetwebstack/blob/4e40cdef9c8a8226685f95ef03b746bc8322aa92/src/System.Web.Mvc/Properties/MvcResources.resx
And I am sure the source code I shared above is MVC 5.x.
So, can any one help? Thanks.
You can get resources using System.Resources.ResourceManager.
So your Code look like below -
if (String.IsNullOrEmpty(fullName))
{
throw new ArgumentException(System.Resources.ResourceManager(typeof(MyProject.MvcResources)).GetString("name"),"name");//replace `MyProject.MvcResources` by full name (with namspace) of your Resource class
}
You can create and user custom HtmlHelper like below.
Example:
Helper:
public static class HelperClass{
public static string InputHelper<T>(this HtmlHelper html, object key) {
return new System.Resources.ResourceManager(typeof(T)).GetString(key.ToString());
}
}
Use:
#Html.InputHelper<MyProject.Resouce.MyResouce>("name")
Hopefully It's help you.
Yes the post is old, however this came up in a search today whilst I was looking for the the actual resx file for System.Web.Mvc.Properties.MvcResources (so I could replicate a value in it)
The reason that you cannot access System.Web.Mvc.Properties.MvcResources directly is that it's declared as internal.

Laravel 4: how to inject another class in a eloquent model

I'm trying to use the built-in laravel's Ioc container to inject a PageManager class inside a Page model and I'm a little lost.
What I'm trying to achieve is something like that:
class Pages extends Eloquent {
public function __construct(PagesManagerInterface $manager, array $attributes = array())
{
parent::__construct($attributes);
$this->manager = new $manager;
}
public function saveToDisk()
{
$this->manager->writeToFile();
}
But I obtain this error:
ErrorException: Argument 1 passed to Pages::__construct() must be an instance of PagesManagerInterface, none given.
I tried to add this in app/start/global.php:
App::bind('Pages',function(){
return new Pages(new PagesManager);
});
But is seems ignored by the framework, and also i don't know how to insert the $attribute array into this declaration.
I'm a little lost so any help is appreciated!
It's not a good idea to overload a model's constructor because new instances can be spawned behind the scenes through various methods, like Model::find().
When that happens, the dependencies you're asking for in your custom constructor aren't being passed in because the Model class isn't aware of them. So, you get that error message.
See the find() method here: http://laravel.com/api/source-class-Illuminate.Database.Eloquent.Model.html#380-397
See this post by Jason Lewis: http://forums.laravel.io/viewtopic.php?pid=47124#p47124
I think that what you need is:
App::bind('PagesManagerInterface',function(){
return new Pages(new PagesManager);
});
This tells Laravel to inject a new Page object everytime it needs an instance of your PagesManagerInterface wich wasn't passed while creating the model.
In Laravel you can use the IoC Container:
public function saveToDisk(){
$managerObject = app()->make('path\to\class\PagesManagerInterface');
$managerObject->writeToFile();
}

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.
:)

T4MVC and named parameters

I'm running into an error with T4MVC and named parameters. I have a controller:
public class ProductsController : Controller
{
public virtual ViewResult List(int page = 1)
{
// foo.DoSomething()
}
}
It seems T4MVC creates an overload List() as well. The result is that calling
myProductsController.List(3)
correctly executes foo.DoSomething(). But calling
myProductsController.List()
does NOT execute foo.DoSomething() - T4MVC created an empty List() overload.
I've taken T4MVC out of my project, and everything works fine now. But I'd really like to be able to use it - am I missing a setting somewhere?
UPDATE: Ok, I have a real fix now. It's checked into the Codeplex repository. You can get the latest T4MVC.tt by going to here. Before I include that in the next official build, it would be great if you could try it and confirm that it works for you. Thanks!
You're right, there is a problem here. I had not run into this situation before. For a short term quick fix, just get rid of the following code from T4MVC.tt (around line 370):
<#foreach (var method in controller.ActionMethodsUniqueWithoutParameterlessOverload) { #>
[NonAction]
[<#= GeneratedCode #>, DebuggerNonUserCode]
public <#=method.ReturnTypeFullName #> <#=method.Name #>() {
return new T4MVC_<#=method.ReturnType #>(Area, Name, ActionNames.<#=method.ActionName #>);
}
<#} #>
But I'll need to look for a real fix. Normally, this generation happens when the action has no no-param overload. It just needs to detect that an action with all-optional params should basically be treated as a no-param case.

AuthorizeAttribute extension for .net MVC...two problems/questions

my first question is, where do I put this custom extension so that it can be called rather than the default AuthorizeAttribute?
I currently have created a new project that contains all of my business logic within my MVC solution. I have a .cs file within my logic project that contains all of my security classes. I tried adding an extension class to that file and on my controller, it sees the class just fine and intellisense isn't barking at me when I add the attribute, but when I try to compile, I get an error that the type or namespace could not be found. Does the custom attribute need to be housed somewhere special in order to compile?
Second question...could be related to the first: When I'm trying to override the AuthorizeCore method from AuthorizeAttribute, I'm passing in System.Web.HttpContextBase as httpcontext. For some reason visual studio can't resolve System.Web.HttpContextBase. Again this might be related to where I have this class saved in my solution. Or perhaps I need to import a dll somewhere to extend this?
Please advise.
As far as I am aware the class can be in any class library project as long as you have properly referenced the project in your web site. You might want to try recompiling the project -- it sounds like it's able to find the source files, but the code isn't in the DLL.
You probably need to add in a reference to System.Web.Abstractions to your project. While the "base" classes are in the System.Web namespace, they are found in a different DLL.
It works for me,I hope this will help you.
using System.Web.Mvc;
public class CustomAuthorize : AuthorizeAttribute
{
}
In controller
[CustomAuthorize]
public class OrderController : Controller
{
}

Resources