I have a controller
public class JobSearchResultController : Controller {
public ActionResult Index() {
return View();
}
}
the View ~/Views/JobSearchResult/Index
#Html.Raw("<b> TEST VIEW </b>")
And then I register the route
public IEnumerable<RouteDescriptor> GetRoutes()
{
return new[] {
new RouteDescriptor {
Priority = 5,
Route = new Route(
"JobSearchResult",
new RouteValueDictionary {
{"area", "Module.Test"},
{"controller", "JobSearchResult"},
{"action", "Index"}
},
new RouteValueDictionary(),
new RouteValueDictionary {
{"area", "Module.Test"}
},
new MvcRouteHandler())
}
};
}
And I'm calling it in one of the other views
#Html.ActionLink("Click Me",
"Index",
new { controller = "JobSearchResult", area = "Module.Test"" })
Works good, it takes me to the Actual View/Index displaying the plain page with
TEST VIEW
in localhost/Orchard.Web/JobSearchResult URL
Now, my problem is I'm expecting this page to use the current theme / current layout (masterpage) just like a normal content type
How can I do this?
I know theres an [Admin] attribute where you can be able to display in Admin view using the Admin theme but how about if I want to display it in the Client. Is there something like
[Client] //display in Client
public class JobSearchResultController : Controller {
public ActionResult Index() {
return View();
}
}
Thanks in advance!
Yes. You can apply the Orchard.Themes.ThemedAttribute:
[Themed] //display in Client
public class JobSearchResultController : Controller {
public ActionResult Index() {
return View();
}
}
Related
I have created one custom payment plugin and I want redirect to that Plugin Controller
action on submit button of Nop.web View
In the class that implements the IPlugin interface, a couple routes will be defined. Eg.
public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
{
actionName = "Configure";
controllerName = "SomePlugin";
routeValues = new RouteValueDictionary { { "Namespaces", "Nop.Plugin.Payments.SomePlugin.Controllers" }, { "area", null } };
}
You can redirect to these actions by using the following.
public ActionResult RedirectToPlugin()
{
return RedirectToAction("ConfigureMethod", "Payment", new { area = "Admin", systemName = "Payments.SomePlugin" });
}
In a controller I added some logic, right clicked on that controller and added one partial view. Why return View() not going to return my added view. I checked that right clicking on the controller when click on go to view -> showing that view. But by using code return view() not returning my view. Its asking full path of the view.
return View("/Areas/Admin/Views/Search/ShowResult.ascx"); -->working
return View(); -->not working
I need to eliminate full path. Please tell me why my view not returning using this return View().
EDIT :
<%: Html.ActionLink("search", "index", new { Area = "admin", Controller = "search" }, new { style = "text-decoration: none" })%>
AdminAreaRegistration.cs
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName {
get {
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context) {
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { area = "admin",action = "index", id = UrlParameter.Optional }
);
}
}
Global.ascx.cs
public static void RegisterRoutes(RouteCollection routes)
{
AreaRegistration.RegisterAllAreas();
}
Controller :
[HttpPost]
public ActionResult ShowResult(FormCollection collection)
{
return View("/Areas/Admin/Views/Search/ShowResult.ascx"); //working properly
return View();//not working any one tell me ...
}
return View();---------------not working asking full path.please tell me
Thanks
Shouldn't the name of the ActionMethod and view name be identical for you to use return View()? Your ActionMethod name is ShowResult() and the view is named "Test.aspx". The view engine is unable to make that link therefore it asks for a fully qualified name to use return view you have to change the method name to Test or the view name to ShowResult.
I have found a solution for implement multi-tenant in my asp.net mvc project and
I want know if it's correct or exist a better way.
I want organize more customers using the same application handling the web request, for example:
http://mysite/<customer>/home/index //home is controller and index the action
For this reason i changed the default maproute:
routes.MapRoute(
name: "Default",
url: "{customername}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
and I implemented a custom ActionFilterAttribute:
public class CheckCustomerNameFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting( ActionExecutingContext filterContext )
{
var customerName = filterContext.RouteData.Values["customername"];
var customerRepository = new CustomerRepository();
var customer = customerRepository.GetByName( customerName );
if( customer == null )
{
filterContext.Result = new ViewResult { ViewName = "Error" };
}
base.OnActionExecuting( filterContext );
}
}
and using it:
public class HomeController : Controller
{
[CheckCustomerNameFilterAttribute]
public ActionResult Index()
{
var customerName = RouteData.Values["customername"];
// show home page of customer with name == customerName
return View();
}
}
With this solution i can switch customer using customer name and correctly accept requests like this:
http://mysite/customer1
http://mysite/customer2/product/detail/2
...................................
This solution works well but I don't know if the best approach.
Does anyone know a better way?
You can model bind the customer name, and not have to pull it from route values:
public ActionResult Index(string customerName)
{
}
I want to include a drop down list of years across all the pages in my website. I assumed a good place to put this logic was in the layout page (_layout.cshtml). If a user changes the year I want to change my year session (ModelBinder) to change as well. This was so easy to do with ASP.NET web forms, but seems near impossible to do in MVC. I tried a partial view with no luck. Anybody have any ideas?
As usual you could start by defining a view model:
public class YearsViewModel
{
public string Year { get; set; }
public IEnumerable<SelectListItem> Years
{
get
{
return new SelectList(
Enumerable.Range(1900, 112)
.OrderByDescending(year => year)
.Select(year => new SelectListItem
{
Value = year.ToString(),
Text = year.ToString()
}
), "Value", "Text");
}
}
}
Then a controller:
public class YearsController : Controller
{
public ActionResult Index()
{
return View(new YearsViewModel());
}
[HttpPost]
public ActionResult Index(int year)
{
// TODO: do something with the selected year
return new EmptyResult();
}
}
and a corresponding view for the index action:
#model SomeAppName.Models.YearsViewModel
#{
Layout = null;
}
#Html.DropDownListFor(x => x.Year, Model.Years)
And finally inside your _Layout.cshtml you could use this controller:
<div id="selectyear">#Html.Action("index", "years")</div>
and attach a corresponding script which would send an AJAX request when the value changes:
$(function () {
$('#selectyear select').change(function () {
$.post('#Url.Action("index", "years")', { year: $(this).val() }, function (result) {
});
});
});
Consider two methods on the controller CustomerController.cs:
//URL to be http://mysite/Customer/
public ActionResult Index()
{
return View("ListCustomers");
}
//URL to be http://mysite/Customer/8
public ActionResult View(int id)
{
return View("ViewCustomer");
}
How would you setup your routes to accommodate this requirement?
How would you use Html.ActionLink when creating a link to the View page?
In global.asax.cs, add following (suppose you use the default mvc visual studio template)
Route.MapRoute("Customer",
"Customer/{id}",
new { Controller = "CustomerController", action="View", id="" });
Make sure you put this route before the default route in the template
You then need to modify your controller. For the view,
public ActionResult View(int? id)
{
if (id == null)
{
return RedirectToAction("Index"); // So that it will list all the customer
}
//...The rest follows
}
For your second question, ActionLink is simple.
Html.ActionLink("Link Text", "View", "Customer", new {id=1}, null);