MVC 3 Razor - Use script references in Layout page from Partial View - asp.net-mvc-3

I used some jquery in my partial view and realised that I need to have the script reference put in the partial view Again in order for the jquery to works.
I wonder is there any method which I can "call" all the script references in the Layout page so I dont need to duplicate it in the Partial View??
I tried to search for related information and tried to use #section to store the script references, but it seems cant work.
Hope can get some guide here... Appreciate it...

You can put all your script references in a Partial View of its own, and then call #Html.Partial("ScriptReferences") from both the Layout page and the other Partial View that needs the script references.

I normally include/reference all the scripts in my layout view. Although some are not needed on all pages.
The partial views, do not have to worry about a script not available. And it also saves time during development.
The overhead of having all scripts available is minimal. Specially if you merge all .js files in one big minimized script (Chirpy can do that for you). Modern browsers will know it's the same script and not download and parse them over and over.

Script references in Layout page are automatically shared with partial views. You might encounter issues if the Home page (which loads layout) URL and the partial view URLs are not relative ... Use relative URLs to load the partial views and the sharing of script file references and global namespaces would work ...

I found another option that turned out to make my life a lot better:
http://kazimanzurrashid.com/posts/asp-dot-net-mvc3-razor-script-loading-tips

Related

MVC 5 Razor view displays Linq errors but page renders correctly

I frequently get errors such as displayed in the picture below when using Linq in a Razor view. I have obviously added the #using System.Linq include on the top of the page (although it will strangely work without it), but still get the errors no matter what i try.
Is there any way to prevent these errors from appearing? I know i shouldn't be using too much code inside a view, perhaps the best solution would be to build the list in the code, use it here and stop worrying about editor syntax errors?
Razor Linq Error
This kind of problems is often in some way related to the config files. Have you seen this answer?
The type 'IEnumerable<>' is defined in an assembly that is not referenced
You can also try to create a test project and see if everything works as expected there and if it does, compare the web config files for possible differences... Note that there are two of those in each mvc project...

How to go about debugging Durandal

I had a trivial Durandal app working, paused to prototype a capability in an html page related only by the fact that it resides in the same folder, and then returned to the Durandal app only to find that while the shell is constructing the menu, views don't seem to load or at least render. The # url changes but that's it.
I've previously seen this when I had broken JS in a view or in shell.js, but I've tried stripping the routing table down to the home view which is basically static html with an empty view, and it's still happening.
The question is not so much "why are my app views showing blank" as "how does one go about troubleshooting this". There's no barfing, just a white silence. I'm not fond of guessing games and I can't believe a framework so well thought out lacks a strategy for troubleshooting.
Yes, I've seen the http://durandaljs.com/documentation/Debugging.html page. Debugging is switched on in my main.js but I don't know how to proceed from here.
So, where does one normally start when this happens? Some logging seems in order but I have no idea where to put it.
I marked the applicationHost div like so
<div id="applicationHost">
<div style="margin: 100px;">Hello, I haven't been replaced yet.</div>
</div>
and it vanishes right after shell calls router.activate()
OK, I found the specific problem, it was a bad binding in shell.html - the name of a binding target changed and the binding needs to be updated.
But the question of how to find this stuff remains. My app is trivially simple. "Inspect everything and hope you notice any problems" is not going to work on a complex app.
In the absence of any advice I'm starting to think that the only available strategy is to make only incremental changes with constant regression testing.

cshtml and ascx in one MVC3 app

Is there something wrong to use both types of views (cshtml and ascx) in single MVC3 app? We use old-school Master with server-side form. So all views are basically .aspx. But sometimes in case of partial views it is handy to use cshtml instead of ascx.
Is it ok? Or there are some troubles in future with that way?
No answers so far. Trying to answer by myself.
It's OK in general. All works fine. BUT! If you can use only one type (cshtml OR ascx), you have to use only one to make your project more unified.

Creating a MVC 3 Site theme design

I have a MVC 3 web application for which I would like to implement a theme/templating framework (similar to Wordpress or Joomla where you can enable or disable different themes for a site) and would like to find out what is the best way to accomplish this.
My design constraints are this. I buy HTML templates from places like Themeforest or Template Monster and want to make a number of these templates available for use in my web app. The templates you typically buy online all have very different CSS layout frameworks, so it's not possible to just drop a new CSS in your /Content/Themes/ folder and then your views work with this because you also have to make changes to the HTML in the Views. So for every new theme in /Content/Themes I also need a new set of /Views/ folders where the HTML is updated to use the proper CSS classes for that theme.
I know in an ideal world I should just have all the CSS compatible with my html views (ie theme switching is purely based on loading a new CSS), but in this case that is just not possible.
So I'm wondering if there is way to tell MVC where the current default Views folder is? A possible project structure I'm thinking of is something like this:
/Themes/Theme1/Views/(all cshtml view pages for this theme goes here)
/Themes/Theme2/Views/(etc)
I know that I can force the views like this
return View("~/Themes/Theme1/Views/Controller/Index.cshtml")
which can be extended to dynamically determine the theme folder with something like this
return View(currentThemeFolder + "/Views/Controller/Index.cshtml")
but I am wondering if there is a better way to do this? Is there someway perhaps in the global.asax to set the default "Views" folder so that the above is not necessary? There must be an entry point to the views because how do you then specify which _viewstart.cshtml file is called? And if I do go for the above design of forcing/"semi-hard-coding" the views what are the down sides to it?
I also want to say that I have not looked much at Areas in MVC 3 and I don't think that would work here, because my understanding is that it would affect your routing, and would also duplicate your model and controllers folders which I don't want. I have on central Model and Controllers folders, but just need multiple Views folders.
You can extend VirtualPathProviderViewEngine to create your own themeable view engine. Here is how: http://www.singingeels.com/Articles/Creating_a_Custom_View_Engine_in_ASPNET_MVC.aspx

Making a different type of home page in Orchard

I've just started with the Orchard CMS and I can see that I can create a page and have different widgets on them, but I need the homepage to have a different layout.
I'd like to have some sort of different cshtml file for the homepage, but I can't seem to work out how to do it from the documentation. I also need to have four text areas on the home page that aren't on the main page.
Can someone who has done this point me in the right direction?
You should use designer tools module if you are using Orchard 1.1 +. Just search for Shapes in the admin and install the tools. Then you can create Alternates by reviewing the shapes in-browser.
In your case you should be able to create a view with the name: Content-url-homepage.cshtml
This may help you, it's a post I found about making your own themes and is a fairly simple example but covers what you are looking for:
http://www.nogginbox.co.uk/blog/theming-orchard
/Views/Content-11.cshtml
I wanted to display the content on my
homepage slightly differently than
other pages. My homepage has an ID of
11 so I was able to reference it with
a file called Content-11.cshtml. I
could also have created a template for
all blog post content using a file
called Content-BlogPost.cshtml had I
needed to do so.
You can try this to vary the homepage layout: http://weblogs.asp.net/bleroy/archive/2010/12/14/switching-the-layout-in-orchard-cms.aspx
Orchard 1.1 will also give you a homepage alternate out of the box.

Resources