Using T4MVC in real project - asp.net-mvc

T4MVC is cool, but I have a couple of issues integrating it in my project, any help is really appriciated:
I've got such warnings for all my actions (I use SnippetsBaseController as base class for all my controller classes:
Warning 26 'Snippets.Controllers.ErrorController.Actions' hides inherited member 'Snippets.Controllers.Base.SnippetsBaseController.Actions'. Use the new keyword if hiding was intended. C:\projects_crisp-source_crisp\crisp-snippets\Snippets\T4MVC.cs 481 32 Snippets
Is it possible to have strongly typed names of custom Routes, for example, I have route defined like this:
routes.MapRoute(
"Feed",
"feed/",
MVC.Snippets.Rss()
);
Is it possible to replace:
<%= Url.RouteUrl("Feed") %>
with something like:
<%= Url.RouteUrl(MVC.Routes.Feed) %>
Having strongly typed links to static files is really cool, but I use <base /> in my pages, so I don't need any URL processing, can I redefine
T4MVCHelpers.ProcessVirtualPath without tweaking the T4MVC.tt itself?
T4MVC always generate links with uppercased controller and action names, for example:
/Snippets/Add instead of /snippets/add. Is it possible to generate them lowercase?

Can you change your base controller class to be abstract? I think that'll fix it.
See this post which discusses this: http://forums.asp.net/t/1535567.aspx
If you look in t4mvc.settings.t4, you'll see a ProcessVirtualPathDefault method that you can change without touching the main .tt file.
See http://forums.asp.net/t/1532057.aspx. There is suggested fix in there, though it has not yet been submitted (but you can try it).
David

Related

Ember: How to link to sections and actions (without nesting routes)

Let's say that I have a blog where each post can have several sections and comments and I'd like to use a hard-links to navigate and operate on this. There are several samples using some pseudo-code, of course they doesn't work, just demonstring my intends :)
Of course /blog.html#/posts/1 uses PostRoute, PostController etc and uses :post_id for finding object - that's obvoius.
How can I pass (and then access) additional params which doesn't change the controller but I can use them to navigation. ie /blog.html#/posts/1?section=123 should use the same route, controller and view as it was just Post, but I'd like to read the section and just navigate to section with #123
/blog.html#/posts/1/?comments=456 - actually should behave like section from point 1, but navigates to comment and optionally add some class to the container.
Other case: I'd like to go to section 123 AND additionally edit it with link like: /blog.html#/posts/1?section=123&action=edit. Now I'm using a button with an action like {{action editSection section}} and {{#if isEdit}} but I'd like to be able to reflect this in URL and also go to this state from URL (de facto my post can have several different modes not only preview/edit, therefore it should be accesible by the link).
I hope that cases makes sense, TBH I have no idea in which direction should I go. Tried with nested routes, but I'd like to avoid changing the controller. Also have no concept how to reflect the action in the URL...
I'm using Ember 1.1.2
You can use the model method of the route to handle such parameters, separate them from the model parameter and set the appropriate controller state.
Another approach would be to use nested routes that will render un-nested views(and controllers) - as explained towards the bottom here.

Find broken controller actions

How do I find broken Html.ActionLinks?
e.g. Find Html.ActionLink("View", "ViewCustomer", "Customer") where CustomerController.ViewCustomer() no longer exists.
You can use strongly-typed action links from MVCContrib.
<%: Html.ActionLink<Home>(c => c.Index()) %>
These will break at compile-time when you remove a referenced controller method, if you are using compiled views.
If you looking for systematic approach install (can use NuGet) and apply T4MVC.
I did it to my own project - all your magic strings (not only action links, but everything that require strings) disappear across your application. You end up using only strongly typed helpers to eliminate the use of literal strings.
In your case in particular
#Html.ActionLink("View", "ViewCustomer", "Customer")
will become
#Html.ActionLink("Externalize me as well!", MVC.Customer.ViewCustomer())
and if you externalize the one suggested, it will become what you looking for:
#Html.ActionLink(Config.ViewLabel, MVC.Customer.ViewCustomer())
Isn't it a beauty?
I think this supposed to be de facto standard rather than "stringified" approach.
Look to what it does to your project:
In Views:
#Html.RenderPartial(MVC.Customer.Views.YourView)
In controllers:
return View(Views.YourView);
or
RedirectToAction(MVC.Home.MyAction().AddRouteValues(Request.QueryString));
Hope this helps.
The accepted solution does not answer the question. Here's my solution (not sure if it requires Resharper or not).
Click on any broken action (create one if you must)
Hover the red bulb
Hover Inspection MVC
Hover Find similar issues in >
Click custom scope
Restrict your scope (it might tell you 10000's of quality issues)
Search "resolve action" in the results

Where to put reusable HTML code in rails?

I'm writing a rails app that allows users to delete records of various sorts. After pressing a delete button, I'd like to show a confirm dialog using bootstrap. I'd like to use this same dialog in several of my views, so I'll need to include the same HTML snippet in most of my pages.
I'm new to rails and I'm still learning the conventions. Can anyone suggest the best (or standard) place to put the dialog code? Should it be a partial in views/layouts/_confirm_delete_dialog.html.erb, should it go inside application.html.erb, or should I put it somewhere else?
Thanks in advance for your advice,
D.
Within your Views folder, you can create a general folder (called whatever you want). If you have a variable that you need to pass through to the general layout, you can definitely do so, but you will want to make sure that the information that you're passing through doesn't cause conflicts with the fields pulled from the model. For example, if you have two models and one has a public field but the other doesn't then you will not want to have a generic message that is using the public field. However, something like the created_at or updated_at would be okay.
You would use a code similar to,
<%= render 'general/simple_message', :f => f %>
In your views folder, you would have a directory called general and a file called _simple_message.html.erb.

Where should I put XML Builder code?

I am creating a method to generate an XML document via Ruby Builder.
Where should I put the method that created the XML markup? Should it be a method on the model?
I plan to have the XML document pull from multiple models via associations, so I think I need to have it in the controller or a helper, but I would like some in put on the best place.
If it's practical for you, I'd put it in your views folder. This lets you follow the traditional pattern of "load stuff in the controller; render stuff in the view," and has the extra perk of keeping what's probably a messy and very specific method in its own file and out of the way.
Now, I'm not sure if there's a Ruby Builder template format, but you could always just wrap your code in <%= ... %> and treat it like a regular ERB file - should work about the same.
Hope that helps!

Should I make a ImageHelper in this situation?

Hi I'm working with a project (asp.net mvc) where I need to show pictures on one site. They gone have jquery and be surrounded by a div like <div><img/></div>
I'm relatively new on MVC so I'm not sure what ways are the best to work in it yet. Should I do a ImageHelper so i can access it like <% Html.ImageJquery() %> or should i just do it plain in the view
what are your thoughts on this?
I am not sure if there is any single best practice for this case. As you already said you could create an extension method on a HtmlHelper or just put the code in the view.
Because the "added" code is a very simple (just two divs) I would skip extension method and just add the divs in the view. But if the code is actually more complex I would create a helper.
Cheers!
It depends how often you would use the helper. If it is used in a couple of places it would make sense, because it helps you to reduce redundant code.
The other option you have are partials.
I would go with an Extension method on the HTMLHelper , so that it accepts the 'src' value as a parameter and constructs the image tag accordingly.
Regarding Rendering of UI elements , i did read a blog post on 'Conditional Rendering' which i thought you would find it interesting .
please check the link below .
Conditional Rendering
Thanks ,
Vijay.

Resources