ASP.NET MVC 2 parameters throws JS error - asp.net-mvc

My application works fine when I have only one parameter.
e.g.
/Product/Index/2
/Report/Sales/08-2009
But it failes when I add one more part to the url. Let's say I want to add the end month-year parameter to the url routing. Mow it becomes:
/Report/Sales/05-2009/09-2009
I do get both parameters in my action method. I parse it and retrieve the data and pass the Model to the View. With this scenario it throws the client side JS error when I try to access any of the form elements. I get "object expected" error. The same view works fine using just first parameter. What could be the issues here?
I also loose the CSS styles when this error occurs.
Thanks

well, without seeing any code at all this is difficult to troubleshoot, but I'd say it's likely because you are referencing your javascript and css files using a relative path like:
../content/scripts/myjavascript.js
Adding the second url parameter has caused the browser to be unable to find the urls because you have added what looks like an extra level of depth to the url.
You should provide absolute urls to your scripts and css files. An easy way to do this is to use the "ResolveUrl" method like so:
<%= ResolveUrl("~/Content/Scripts/myjavascript.css") %>

Related

How to hide url in angular?

Is it possible to not using angualar routes, because if I defined an routes like:
app.config(function($routeProvider){
$routeProvider.when('/home')
})
my url will look like www.app.com/#home,
I dont want to change url, just www.app.com nice and clean, in this case how to defined template for defferent controller and how to load the template in ng-view, and how to pass url parameter?
any idea?
You cannot do that with the built-in routing mechanism because path is required for every route. However, you can use ui-router library in order to create URL-less states and navigate to them using either code or directive.
I created this plunker to demonstrate how to use ui-router to navigate among states without modifying URL.

Routing errors while processing URL(s) without a http prefix

We render HTML within a certain page and certain links don't have a http prefix (e.g. foo.com/bar) when you click on it throws a routing error. Is there a easier way to navigate to the right URL in such cases
Normally, the link_to takes care of you in this regard. You say html, so I assume we can't use ERB code, so you'll have to hardcode the links into the HTML.
Make sense?
Of course without any html code or routes.rb code, I can't tell you whether or not there isn't anything wrong with your existing code.
Why do you need to use absolute links within your application? Navigating to just "/bar" on that case would be fine.
If you use the link_to helper function on your views, it should rely on url_for which by default generates relative paths.
If you need absolute paths, you can specify a default host on your application (How do I set default host for url helpers in rails?) or specify it on the host option for the url_for function (http://apidock.com/rails/v3.2.13/ActionView/Helpers/UrlHelper/url_for).

How to separate javascript libraries and calls in the Rails 3.1 asset pipeline

I'm trying to get the hang of the whole asset pipeline thing, and read the guide and several tutorials about them. But one thing that doesn't become quite clear is wether I should view my javascript asset files as a library or a place to put code that is actually run i.e. $(document).ready. Because by default all the javascript files are included, and it would be weird to have several $(document).ready's in there, not to mention that you don't want the $(document).ready function for every page to be run in the first place. What would be the way to go at this? Use my asset files as a library and put actual calls in my views (ugly)? Or is there a better way to do this?
I too ran into this issue. In a large project you can have somebody put code into document ready to, for example, add a click function to each li within a div with class container.
Now we could all argue that the above code would be too generic and of course may affect li tags in other parts of the application, but the bigger the project, the more likely it is that you will run into a conflict like this leading to unexpected behaviour.
I for one am uncomfortable with a whole bunch of document ready functions running for each and every page loaded. My solution is not necessarily the perfect one, but it's one that I have taken up and will share with you.
In the body tag of each page I add data elements signifying the controller and the action. I then have one document ready script that looks for a class named after the controller with the name Ready appended e.g. HomeReady. It will then call a method on this class (presuming it exists) named after the action. So in your asset coffee file you could write:
class #HomeReady
#index: ->
alert("Hello")
#show: ->
alert("Goodbye")
This allows control right down to the action level. When I came across your question I decided to package this solution into a gem as I have already used it in several projects. You can find it at: https://github.com/intrica/rails_document_ready
If you absolutely don't want a certain piece of initialization code to be run unless the current page is a specific controller/action, then you can try adding an empty element on the page with an id built from that info like "posts_index" using these two helpers:
"#{controller_name}_#{action_name}"
Then in your javascript you can wrap the code inside an if statement that checks for the existence of an element with the appropriate id.
edit: Here's an example of the js partial that I mentioned in the comments.
show.html.haml
= render 'map'
map.html.erb (I normally use haml but it's easier to write js in erb)
<script src='http://www.google.com/jsapi' type='text/javascript'></script>
<script type='text/javascript'>
...
</script>
It's probably not as clean as it could be and it doesn't get the benefits of being part of the asset pipeline but I don't mind because it's only something that gets included on a specific page.

Rails validation

I am suffering a issue with rails server side validation. Can some one help me out from this?
situation is :
I am creating dynamic for and its elements also dynamic.My application will generate the some HTMl code. Which can we use in any form or blog..
I applying the server side validation. But due to dynamic elements .I am not able to store the last entered value in to the elements. AS we normally does in PHP if user input something wrong we don't put the field empty. So I need to find a mechanism which fills the older values into the elements,If something went wrong.
This is the code into controller which is I'm using to show the form :
render :layout => false,:template=>'buildders/rander_form'
and view of rander_form.html.erb has
<%= render :file=>RAILS_ROOT+'/public/forms/form_'+#form_name+'.html.erb' %>
where #form_name is a dynamic form name(which have HTML code).
Can some one help me?
don't put erb files in public, people can download them by entering the file path in the url
also why not move that code out of the erb template into the controller?

Using MVC Routes as Shortcodes

We have been trying to implement shortcodes on an ASP.NET MVC web app that allow users to uniquely invoke a given article/page using an assigned short code.
For e.g.: www.mysite.com/power would map to an actual URL: www.mysite.com/Power/Home/.
I have created various routes throughout the site that map these shortcodes to various actions and controllers within the application. From a shortcode/route point of view, everything is working great.
I, however, noticed a couple of interesting things. I have hyperlinks that I use Url.Action method to generate the URL pointing pages. Many of these pages also have short codes associated with them. For e.g.: I have a link that says:
Go to Power page
This is a page that also has the previously mentioned short-code assigned to it. When I use Url.Action, I ideally expect it to create a link as /Power/Home/Index or /Power/Home, but since I also have a route constraint mapped to it, it now generates the link as /power.
Is there a way I can just use the actual link URL when generating links? I only want short-codes when I am sending out emails etc. I want the site to generate actual URLs.
This may or may not be possible, but I wanted to see if there were any ideas out there that I could use.
Anup
Index and Home are likely defined in your route table as defaults for the Action and Controller element. When you generate the Url it wont include the defaults if they aren't needed.
You could write your own Action overload or helper, which would allow you to take more direct control of the generated URL or action link. You could approach it from two different ways: 1) a helper to generate short-code specific urls and links, and/or 2) a helper to generate the full url and/or link. If Url.Action is returning the short-code version due to your routing configuration, I'd think a good place to start would be the second option, creating a helper/extension method that will generate the full url for you.
Here's how I solved this:
Instead of naming a route with short code to point to the action url, I made the route point to a different Controller action which would then redirect to the actual route that I want it to.
For e.g.: Originally I had the code "power" defined in the route table such that it would point to www.mysite.com/Power/Home.
Now instead of pointing it to that action - Index, controller - Home, area - Power, I make it resolve to: action - Power, Controller - Home, Area - ShortCode.
In the controller now, I simply do a RedirectToAction("Index", "Home", new { Area = "Power" });
This ensures that the actual links to /Power/Home do not resolve to the shortcode "power".
This is a simple fix increased the work by a little bit, but works like a charm.

Resources