Is this possible?
I'd had to clutter my route registrations with random actions that I need for specific reasons that don't need any custom constraints or anything.
I want to do something like Admin/Categories/{action} where action can be anything and the controller is CategoryController.
Is this possible?
routes.MapRoute(null, "Admin/Categories/{action}",
new { controller = "Category" });
If you setup an Admin Area, then the categories controller in that area will be accessed via /admin/controller...
Areas, http://msdn.microsoft.com/en-us/library/ee671793.aspx
Related
I'm making a search for joomla, after clicking search button I am getting this url:
index.php?searchword=aa&task=search
How can I create a view or task for it?
If you use the basic joomla search component you will find the views in
/components/com_search/views/search/tmpl
If you edit the view then its advisable to use template overrides, to ensure you will not lose your views on upgrade : http://docs.joomla.org/How_to_override_the_output_from_the_Joomla!_core
addition :
If you are building a component and you want a task to execute like that, then use this in your YourComponentName.php.
$controller = JController::getInstance('FrontendSuite');
$controller->execute(JRequest::getVar('task'));
$controller->redirect();
And add the task as a function in your controller.php. You will get something like this :
function search(){
$searchword = JRequest::getVar('searchword');
//Do your magic
}
As Valentin pointed out just below, you will need to add option=com_yoursearchcomponent to your URL for Joomla to call your component.
Adding Views to your component is explained quite well in the link Valentin posted below, http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5/Adding_a_view_to_the_site_part
Hope this helps,
Good luck
Your URL will be like:
index.php?option=com_yoursearchcomponent&task=search&keyword=xxx
So you need to create a component. Have a look at the Developing a Model-View-Controller Component.
Then you will have in your controller or subcontroller the task search which will call the view search, where you will have the appropiate template for the view.
We are using MVC 3 to build a Web site with dynamic menu options so that users only see menu options (action Links) that they are allowed to see based on group and individual privileges.
How can we add dynamic menu options (links to partial views) at run time?
Do we hard code all the links of all the partial views and turn of the ones that are not required using a visibility option?
Can we add the links dynamically from a database?
Let me clarify.
WE have admins that have access to all menu options like Manage Users, Manage Groups, Manage Suppliers, Manage products and Manage Orders.
We have regular sales staff who only need Manage Supliers and Manage Orders.
So based on this we only need to show the links that say Manage Orders and Manage Supplier. Hence they dynamic nature of the links I am trying to set up.
We have the permissions set up in the DB.
Jawahar
I am not sure I entirely understand what you mean when you say "links to partial views". You never really have a hyperlink to a partial view. The two possibilities I can think of are either you want to know how to embed the partial view conditionally, or you want to have hyperlinks to a controller action which returns the partial views.
In the first case, you can just put the #Html.RenderPartial call inside of an #if (myCondition == true) block. With that, the partial view will only be displayed if the condition passes.
In the second case, you can just always call the controller action. In your controller, only return the PartialView if your condition matches. Otherwise, return null.
I found a way of doing this using Method extension with IPrincipal
public static bool IsAllowed(this IPrincipal p, string menuid) {
if (p.Identity.IsAuthenticated) {
//Code here to verify privillegs against Database
}
return false;
}
This would keep it fairly neat in you Layout.cshtml.
#if (User.IsAllowed("menuchoice1")) {
...
}
#if (User.IsAllowed("menuchoice2")) {
<a href="#Url.Action(...)>...</a>
}
Hope this help others looking for similar options
I have a C#.Net web app and I am trying to access one of the HTML/ASP Text Boxes in the Controller for the Edit View of my Proposal model. In a non-MVC app, I was able to do this using Control.ControlCollection.Find(). Is there an equivalent for a MVC3 project?
You ask for an equivalent of Control.ControlCollection.Find() in MVC?
In MVC your controller is not aware of controls.
The controller just receives data via parameters and returns data via the function result.
What do you want to do with the control in your controller code?
If you want to access the value, you should bind it to a parameter:
View:
<input name="MyControl" type="text" />
Controller:
public ActionResult MyAction(string MyControl) {
// MyControl contains the value of the input with name MyControl
}
The MVC pattern was designed to keep things separated.
The View has no knowledge of the controller at all
The Controller only knows that a view exists and what kind of data that it needs. It do not know how the data is render.
Hence, you can never get information about controls/tags in the view from the controller. You need to use javascript/jQuery in the view and invoke the proper action in the controller.
In an MVC-application you don't have controls like in a webform-application.
In MVC you collect your required data in the controller and pass it to the view.
Typicaly the view is a HTML-page with embedded code.
In opposite to controls in webforms which produce HTML and handles the post-backs in MVC you have to do all this manually. So you don't have controls with properties and events wich you can access easily in the controller and you have to handle all your posts with your own code.
Thats sounds as it is a lot of more work - and indeed it could be if you implement the behaviour of complex controls - but MVC applications are much better to maintain and you have 100% influence to the produced HTML.
Well probably i am late for this but it should help others in future...u can store ur value in hidden field in view and then access that value in controller by following code..
Request.Form["hfAnswerOrder"].ToString();
Point - hfAnswerOrder is the ID of the hidden field
My Control in cshtml page..
#Html.Hidden("hfAnswerOrder", Model.Answers.ToList()[0].AnswerOrder)
Imagine I have a form (Page1.cshtml) with 1 link (LinkBrands). I also have a controller for Page1 (Page1Controller) and one for brands (BrandController). When the user clicks the link what is better to do:
LinkBrands-->Page1Controller-->BrandController (Page1Controller's action will redirect to BrandController)
OR
LinkBrands-->BrandController
Not sure what route is better. Any suggestions?
Usually you don't have a single controller per view. You would use multiple views or partial views all calling actions on the same related controller. I assume Brand is a separate entity from whatever else Page1 is trying to display, therefore it should probably use the BrandController directly but since there really isn't enough information to go on with your example as to what page1's function is I couldn't say what you are trying to relate.
If you need to capture information from the brand link as it relates to page1 then sure have it collect that in page1controller first before redirecting to brandcontroller to display a new view.
What does your Page1Controller do?? It seems as though you are defeating the purpose of the Model-View-Controller architecture and trying to form it back to the WebForms method with code-behind.
So without seeing what exactly your controllers are doing, I'd say your second option is best.
It all depends.
If you have to execute any logic in Page1Controller (saving data for instance) before displaying the second page, then you need to go by Page1Controller, and then go to BrandController:
LinkBrands-->Page1Controller-->BrandController
In case you just need to redirected to the second page (you do not need anything from Page1Controller, you do not need it to perform any action, and you can create a the model for second page in BrandController) then go with the second option:
LinkBrands-->BrandController
Hope this helps.
I'm trying to do the following with ASP.Net MVC 3:
I have a lot of "flat pages", which are basically html documents with no dot.net code attached.
I want to be able to request these pages through routed URLs, but I do not want to manually add each url to the routes.
So my question is: Is it possible to define a default route, which uses the same controller / action, but returns a view based on the URL requested ?
e.g. /home/about and /profile would use the views /home/about.cshtml and /profile.cshtml
but both would use the same controller and action, which pretty much just goes:
return View();
The reason: I'm doing all the pages of the site, which require dot.net code. However another person is doing all the "flat pages" (informative pages, etc.).
I want him to be able to add new pages, by just adding a cshtml file (like he would with webforms creating aspx files, with no code-behind)
This is necessary because I'd otherwise have to edit global.asax each and everytime he adds a page, which is quite often.
If this is not possible, I'll have to stick with webforms, which I really don't want to :-(
You can make an action that takes as a parameter the name of the View; Something like this:
public ActionResult StaticPage(string viewName)
{
return View(viewName);
}
Then define a route so the viewName isn't a parameter but instead is part of the URL:
"/Static/{viewName}"