MVC3: The name 'functionname' does not exist in the current context - asp.net-mvc-3

I have both web form and MVC3 at my web application. I have a function which works fine under web form. I tried to reference it in MVC controller.Because I need this function's return value in my MVC controller.So I put function's inherits namespace under MVC controller. But it says 'The name 'functionname' does not exist in the current context.'
For example: I have a function name 'getClaimValue' under PortalUserControl:
namespace Site.Control {public class PortalUserControl:PortalViewUserControl{ public string getClaimValue(){...}}}
so I put using Site.Control under my MVC controller
using Site.Control ; namespace Site.Areas.Account.Controllers{public class AccountController:Controller{[HttpGet]public ActionResult SignIn(){string claimValue=getCliamValue();} }}
So I get red line under 'getClaimValue()' says 'The name does not exist in the current context.'
So how can I make it work?
Thank.

You're confusing master pages with base classes. A page does not inherit from a master page. A master page provides templated presentation and layout, but does not usually have public methods. You may want to create a base class for your controller and put your method there.

Related

Call controller within another controller - CodeIgniter

I need to call a controller say 'faq_view' inside admin controller as the URL structure admin/faq_view like this how I can do this?
e.g:
site.com/maincontroller/function
site.com/maincontroller/othercontroller/function
Then, just redirect the page. Else if you want to just call the function, call it via AJAX.
It depends what you exactly want to do. If you want to just invoke the function, its not the right way. Controller as it defines itself controls the flow of the pages that comes on sequence. Controller is responsible to send commands to its associated view to change the view's presentation of the model.
So, if you are saying you want to call controller within another controller, that should mean you are about to redirect to another page.
Updated answer:
Just assume you have new_function on maincontroller that calls the function from othercontroller. The function does not need to be defined on othercontroller.
Add the following line on routes.php.
$routes['maincontroller/new_function'] = 'othercontroller/new_function';
Now, you can call the function of othercontroller as maincontroller/new_function.
You can always call a controller inside another controller, but this only works for calling one controller as far as I have tried. Let's say you are trying to load a controller inside a controller. You can try this:
$this->load->library('../controllers/myothercontroller');
Then do this:
$this->myothercontroller->function_name();
That's it! You can now access any function inside myothercontroller (controller) in your current controller. I hope this helps too.
Your controllers are part of the presentation layer and should not contain application logic. That means you should never need to call a controller from another controller, instead refactor your application and move the domain logic to the model layer.
Now if you have a method that you need in multiple controllers, say for example you need a template method that automatically adds your header and footer views.
If that is the case, create a base class that your controllers extend.
If you are talking about just a routing issue, then just use the routes file for that. I don't like the CI automatic routing and it should be avoided as it will result in duplicate URLs for the same resource.

codeigniter class name convention

When I'm developing in NetBeans I have a lot of confusion due to the amount of tabs open. In most cases the controller has the same name of the view or model.
In the convention style guide they say that you can prefix a controller file name with a custom suffix, but doesn't work.
My question is, there is any chance to end all the controller files with "_controller"?
In my example the class is class Verify_login extends CI_Controller { and the file is named verify_login.php. Tried with controller.verify_login.php like they say in the guideline but as I say, doesn't work. Lots of confusion in codeigniter's documentation.
Since the controller is the only thing exposed in the URL, I usually name my views and models with an indicator like "user_view" or "user_model". The controller would just be "user" and in this way I always know which file I'm working on.

ASP .NET MVC 3 + Calling an HttpPost Action Method From a Different Controller

I am currently developping a full-web application under VS2010 and I am using the ASP .NET MVC 3 framework.
Here is a simplified overview of my application :
I have implemented a controller Ctrl1.
Ctrl1 contains a HttpGet action method ActMeth1.
The Ctrl1Views folder contains a view View1.
I have implemented a controller Ctrl2.
Ctrl2 contains a HttpPost action method ActMeth2.
ActMeth2 returns a view View2 included in the Ctrl2Views folder.
Ctrl1 and Ctrl2 are in the same namespace.
I want ActMeth1 to call ActMeth2 to perform some logic and then to return View2.
Here is the source code of ActMeth1 :
public ActionResult ActMeth1()
{
Ctrl2 myCtrl2 = new Ctrl2();
return myCtrl2.ActMeth2();
}
Unfortunately ActMeth1 returns View1.
Does someone can give me an explanation to this fact ?
Thanks in advance for your future help
Instantiating a controller's action method in another controller's action method is inviting trouble in the long run.
You can use tempdata, or pass the data via route dictionary of RedirectToAction.
I think you better reorganize your logic
As you are trying to do this logic in server side anyway,
a. Create a service that does the work for both the controllers
b. make the view shared between both the controller actions or create a partial view for the common html
c. Call the appropriate service method and render the shared view
You could do:
public ActionResult ActMeth1()
{
Ctrl2 myCtrl2 = new Ctrl2();
myCtrl2.ActMeth2();
return View("~/Views/Ctrl2Views/View2.cshtml");
}
I'm not sure you should be instantiating controller 2 from inside controller 1 though...

How to set a custom URL path for a controller without creating new routes?

I wonder if there is attribute (built-in or some open source) for me to tag my controllers with the specific URL segment I want it to use, as in:
[MagicUrlRoute("status")]
public class InternalNameNotToBeRevealed : Controller
{
public ActionResult Show()
{
...
}
}
This way, instead of "/InternalNameNotToBeRevealed/Show" being what the user sees, it will be "/status/Show". This might be nit-picking, but it bothers that I have to use the controller class name as the official URL path.
Now, I do understand I could create a custom-route on global.asax, but that will be a lot of work for hundreds of controllers.
I found this very handy library to do exactly that, but only for actions:
http://maproutes.codeplex.com/releases/view/39888
I appreciate any suggestions.
You could have a listing of the mappings and just call MapRoute in a loop to register all the custom mappings. The mappings could be a dictionary, or you could even scan all your controllers once on App_Start, collect a custom attribute value and then use those to build the mappings. However, I'm not sure how well that would perform for a large number of mappings.
If you wanted a higher-performance mechanism, you'd have to create your own Route. You should be able to do this by inheriting from System.Web.Routing.RouteBase and overloading GetRouteData and GetVirtualPath to do the mapping. When constructing RouteData, you can just provide the existing System.Web.Mvc.MvcRouteHandler as the route handler, and as long as your route data contains 'controller' and 'action' values, it should continue down the MVC pipeline. Then just use the Add method on RouteCollection to add your route. You can take a look at MapRoute in System.Web.Mvc.RouteCollectionExtensions for some insight on how MVC adds it's route.

MVC - helpers and Joomla

I'm being confused, I have a function that needs to update some table, I have placed it inside controller, however now I have found that I will need to use it inside other 2 controllers.
What is the best practice to place the function that making updates and where to place it and how to call it?
maybe helper?
Did you create your models by extending JTable? In this case, just add the method there. It's perfectly ok to have business logic in the model such as "increment all rows of this user id by one" (static method) or "split up this name and save it into columns first name & second name" (normal method).
The helper would need to be relevant to the controller, as it will be applied to all controllers. I will assume that not all controllers would see meaning in calling the Update.
You could create a static class with a static method that would update your table.
etc Add a file to the Models folder and then create the following class.
public static CalledFromMultipleLocations
{
public static void UpdateMyTable(string somedata)
{
//Do you update code.
}
}
Without knowing your business object model its hard to really provide a solution.

Resources