MVC3 Razor: Is it Possible to Render a Legacy ASCX? - asp.net-mvc-3

With the Razor view engine in MVC3,
Is it possible to render a legacy ascx?
I was expecting to be able to do something like:
#Html.RenderPartial("Footer.ascx")

Yes. Try this instead:
#Html.Partial("Footer")
or
#{ Html.RenderPartial("Footer"); }

Just wanted to add that I haven't seen a lot of people posting this solution:
Html.RenderAction("Footer", "Home");
This is better practise if you are using MVC, because you can specify any data you need in the controller instead of trying to manage it in a free-floating partial view. Very beneficial if you use a BaseController class to initialize all your calls.
public class HomeController : Controller {
// ...
[ChildActionOnly]
public PartialViewResult Footer() {
// do work
return PartialView();
}
// ...
}

Related

How to forbid View

I have controller
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult _Nasljede()
{
return PartialView("Nasljede");
}
and Index action load _Nasljede action with javascript so when i put in browser address bar
..domain/controller#nasljede my page is working.(PartialView _Nasljede is loaded in Index View).
how can i forbid ..domain/controller?
is this even possible in mvc?
If you have any question please ask. i dont know if i explain my problem correctly.
I think this isn't possible with mvc. I solved my problem with jQuery.
if (!window.location.hash) {
window.location.replace("somewhere");
}

static content pages in an MVC web application

I have just created my first MVC 3 project for a database search using EF db first, but the search is only a part of a big website most of the pages will just contain some text and images.
My question is basically about these pages which on the website would be .aspx, and the code behind would have nothing at all.
They use a master page and some user controls - my guess is that's the reason our front end person made them aspx not html.
I need to convert/include her pages into my project (I don't want to go back to stored procedures and listview after having used EF and Linq, plus I don't have time).
I know of one possible way: create a controller for each of the main menu items, then add ActionResult named for each of the submenu items returning View(), then create respective views.
public class LearnAboutStandardsController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult ITSStandardsBackground()
{
return View();
}
public ActionResult ResearchInitiatives()
{
return View();
}
So my static content pages will become Views.
It's working, I just want to do it for the rest of the pages and modify the links in the text of these pages.
Is there any other way to handle these pages?
There is no logic behind these pages.
I know this was not a perfect project for the MVC pattern with so much static content, but I had my reasons for it.
I handle this with an "StaticContent" controller:
StaticContentController.cs
public class StaticContentController : Controller
{
public ActionResult About()
{
return View();
}
public ActionResult Services()
{
return View();
}
public ActionResult Portfolio()
{
return View();
}
}
Add the code below your route config to handle the static routes:
routes.MapRoute(
"StaticContent",
"{action}",
new { controller = "StaticContent" },
new { action = "About|Services|Portfolio" } // Add more here
);
You're set.
If you need more pages just add the action in the StaticController and adjust your StaticContent MapRoute.
Personally, I would have controllers with simple actions that just render views. That way if you do add more features later you're already set up. And if you want to add security or caching it's a lot easier and more consistent.
You can still use WebForms (with the new Friendly URLs feature if you want "pretty" URLs) for the "static" pages. Or you can use Web Pages with Razor and create CSHTML files for the static content.

Declare ViewBag on controller

I'm using ViewBag.Message with the same message several times into the methods on the controller.
It is possible to declare ViewBag.Message on the top of the class, so can be used in the whole controller without repeat the code?
Assuming Razor syntax you can achieve this with.
#{string pageMessage = ViewBag.Message.ToString();}
then pageMessage is a local variable available to the page, for example:
<h1>#pageMessage</h1>
EDIT
ViewBag is a dynamic object which is a member of the Controller base class so to just specify this once in the whole controller you could put something in your controller constructor.
public class MyController : Controller
{
public MyController()
{
ViewBag.ViewTime = DateTime.Now.ToString();
}
// rest of controller code
}

Get current controller in view

I have a View - _Edit which lives in News M/V/C.
I reuse the V/M via the CategoryController as:
return PartialView("/Views/News/_Edit.cshtml", model);
How from within the View - _Edit can I alert the controller name?
When I:
alert('#ViewContext. RouteData.Values["controller"].ToString()');
The Value is: News
However, the URL is: /Category/foobar
Is there a way to get the value 'Category' to alert? thanks
I have put this in my partial view:
#HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()
in the same kind of situation you describe, and it shows the controller described in the URL (Category for you, Product for me), instead of the actual location of the partial view.
So use this alert instead:
alert('#HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()');
I do it like this:
#ViewContext.RouteData.Values["controller"]
Create base class for all controllers and put here name attribute:
public abstract class MyBaseController : Controller
{
public abstract string Name { get; }
}
In view
#{
var controller = ViewContext.Controller as MyBaseController;
if (controller != null)
{
#controller.Name
}
}
Controller example
public class SampleController: MyBaseController
{
public override string Name { get { return "Sample"; }
}
Other way to get current Controller name in View
#ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
Just use:
ViewContext.Controller.GetType().Name
This will give you the whole Controller's Name
You are still in the context of your CategoryController even though you're loading a PartialView from your Views/News folder.
You can use any of the below code to get the controller name
#HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
If you are using MVC 3 you can use
#ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
For anyone looking for this nowadays (latest versions) of ASP.NET Core MVC, you can use:
#Context.Request.RouteValues["controller"].ToString()

MVC3 Routing Issues - How to re-use a View for all Controller Methods?

I'm trying to implement a common controller in MVC3 to return various JSON feeds, example -
public class AjaxController : Controller
{
public ActionResult Feed1()
{
ViewBag.Json = LogicFacade.GetFeed1Json();
return View();
}
public ActionResult Feed2()
{
ViewBag.Json = LogicFacade.GetFeed2Json();
return View();
}
}
This class has 30+ methods in it, the problem is this requires implementing an IDENTICAL View for each of the Controller's methods (sigh) that writes out ViewBag.Json.
I'm assuming this is a routing issue but I'm struggling with that. The following didn't work -
Tried setting ViewBag.Json then using RedirectToAction() but that seems to reset ViewBag.Json.
Note JsonResult is not appropriate for my needs, I'm using a different JSON serialiser.
So the objective here is to maintain one View file but keep this class with seperate methods that are called by routing, and not a crappy switch statement implementation.
Any help appreciated.
Use the same view and just specify the name. You can store in the controller's view folder, if only used by one controller, or in the Shared view folder if used by more than one.
return View("SharedJsonView");
Another, perhaps better, solution would be to create your own result -- maybe deriving from JsonResult, maybe directly from ActionResult -- that creates the JSON response that you need. Look at the source code for JsonResult on http://www.codeplex.com/aspnet for ideas on how to do it.

Resources