I am just beginning to move from ASPX C# web forms to MVC Razor.
What I am trying to do is use the MVC global much as I did in master page code behind.
The objective is to define page title and then assign a specific image to the _Layout.cshtml.
In the aspx web pages master code behind I used Case (switch)statement like this:
string Title = PageTitle;
switch (Title)
{
case "Quality":
imgPix.ImageUrl = "images.picureA.gif";
break;
case "Services":
imgPix.imageUrl = "images.pictureB.gif":
break;
-- and so on for 8 various pages --
}
This works very well to load images on Page_Load in the aspx web pages.
I want to do this in the MVC global if possible, any suggestions are appreciated.
Related
I have built the tree view using third party javascript plugins. I have also use the web templates for my asp.net MVC 3 application. And use Layout view linking to lot of css and javascript.In my category view I want to display the tree view. But the due to script of layout view the tree view is interrupted and not displaying properly. When I put Layout = Null, it shows properly. How can I set priority to the link of script and css link for displaying tree view properly
Take a look at ClientDependancyMvc NuGet package.
It allows to set priority to clientside resources like this:
Html.RequiresCss("Bootstrap/bootstrap.min.css", "Content", 2);
Html.RequiresCss("Bootstrap/bootstrap-responsive.min.css", "Content", 3);
Html.RequiresJs("jquery/1.7.1/jquery.min.js", "googleCDN", 1);
Html.RequiresJs("jqueryui/1.8.18/jquery-ui.min.js", "googleCDN", 2);
Where the last parameter is priority of inserting element into page. More info here.
Can I set focus to a control from a Controller when calling a View?
(I understand the typical best practice is to use jQuery to set focus to a control when the page is loaded.)
The controller's job (one of them) is to set up a view model which gives the view enough information to render correctly. In other words, the controller and the view should only be loosely coupled.
Here's one way to do it. This is somewhat decoupled though it could be done more elegantly. You still need JavaScript to perform the client-side scripting, but the script is generated based on a value in the view model.
Controller
public ActionResult Foo(){
var model = new MyViewModel();
model.SelectedItem = "FirstName";
return View( model );
}
View
#model MyViewModel
#Html.TextBoxFor( o => o.FirstName )
#if( Model.SelectedItem != default( string ) ){
<script>$("##(Model.SelectedItem)").focus();</script>
}
One thing you have to learn about web development is that you can have all the technologies in the world on the server, but in the end, those technologies have to generate standard (or what passes for it) Html, CSS, and JavaScript.
That means there is no special magic that can be done on the server to automatically do things on the client. Some frameworks can automatically generate code to do this for you, but it still must be done as standard html/css/js in the end.
MVC only renders standard, plain HTML. Webforms will do a lot of things for you, but in the end Webforms has to generate standard HTML as well. It does this by auto generating javacript that gets included in the page that, on load sets the focus.
MVC doesn't do any of those things for you, so you would basically have to do the same thing, but you would have to write it. It's relatively simple using some simple jquery.
I have a custom CMS built with ASP.NET WebForms (you can see it in action at Thought Results). Now I want to build it using ASP.NET MVC 3 (or even 4). I don't want to change the architecture that much, therefore, I need to dynamically load a Razor View, and dynamically run a Model Loader method, and give the model to the view dynamically, then render the view, and return the result rendered string, all done in server.
In ASP.NET WebForms, my code is:
string renderedString = "LatestArticles.ascx".LoadControl().GetReneredString();
Now, I'd like to be able to write a code line like:
string renderedString =
"LatestArticles.cshtml".LoadView().BindModel("ModelBinderMethodName").Render();
I know about many questions about rendering a view (view to string), but I didn't find what I want.
You may checkout RazorEngine.
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}"
Using ASP.NET MVC3 I created a new Razor view and gave it the same name as the existing .aspx view that I had been using. I noticed that controller continued to pick up the .aspx view (which has the same name as the action) which is pretty much what I expected. I then renamed the .aspx view and action picked up the razor .cshtml view.
So if I have two views called myview.aspx and myview.cshtml and an Action called MyView() that does a return View(), it will pick up the myview.aspx view and return that.
How does MVC3 decided which view-type to default to?
Is there a way to change this default behavior to prefer a razor view over an .aspx view?
Everything stems down to the order of view engines in the ViewEngines.Engines collection. Here's how the ViewEngines static constructor looks like (as seen with Reflector in ASP.NET MVC 3 RTM):
static ViewEngines()
{
ViewEngineCollection engines = new ViewEngineCollection();
engines.Add(new WebFormViewEngine());
engines.Add(new RazorViewEngine());
_engines = engines;
}
which explains why WebForms is the preferred view engine.
So you could perform the following grotesque hack in Application_Start to inverse the preference towards Razor :-)
var aspxVe = ViewEngines.Engines[0];
var razorVe = ViewEngines.Engines[1];
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(razorVe);
ViewEngines.Engines.Add(aspxVe);
I would imagine its down to the order in which view engines are registered. Earlier registered view engines will be queried first. If you want to change the order:
ViewEngines.Engines.Insert(0, ...);