Note that there are some explanatory texts on larger screens.

plurals
  1. POURL Routing issue with Javascript (Jquery) call to MVC3 Controller/Action/ID, returning 404, should get JSon
    primarykey
    data
    text
    <p><strong>Question:</strong> How do I code the URL passed to javascript so that in the built/deployed version the code will go to the controller/action with the id and return the JSon data, like it works in the Visual Studio environment?</p> <p><strong>Goal:</strong> to return dates to the datepicker, because they will be disabled dates. I expect a return of JSon array of strings that are dates that are then fed into the datepicker.</p> <p><strong>Relevance to others:</strong> Returning JSon data from a call to Controller/Action/ID seems like it would be a common thing to do. I see other questions regarding routing issues, as well as JSon, and jQuery seems to be a popular way to enhance web development.</p> <p><strong>Background:</strong> I have a jQuery-UI datepicker that calls a Controller/Action/ID and returns json data to the datepicker. It works perfectly when running within Visual Studio, but fails when built and deployed to the Dev environment. The code passes the URL that is generated and passed to the jQuery is: /Secure/Validation/getDisabledDates/999</p> <p>We are using Umbraco, with a directory called /Secure within the Umbraco area to hold our MVC code. The URL that the code tries then generates on the Dev environment is: /Secure/Validation/getDisabledDates/999 If I watch the call and the errors in Firebug, I see that an error for the following: POST <a href="http://dev.ourcompanyname.com/Secure/Validation/getDisabledDates/999" rel="nofollow noreferrer">http://dev.ourcompanyname.com/Secure/Validation/getDisabledDates/999</a> 302 Found (if I investigate further, I find it was really a 404, that got sent to the error page, and that's what was found.)</p> <p>I'm using @Url.Action in the view to get the correct (hopefully) URL to the action, but when the javascript request is made in the Dev environment, the return is a 404. The code gets into the javascript code and displays the URL in an alert, which looks right to my eyes. The code does not seem to get as far as the MVC controller (I tried putting a messagebox there).</p> <p>This seems to be because the controller isn't actually a distinct, real page within a directory, because the MVC code is compiled into a DLL. I've heard this and it makes sense, but other people in the world seem to be calling controllers, so I'm not sure what my problem is here.</p> <p>Originally there was only one set of dates, so some code calls only say GetDates, but then it was broken up by Widget, so calls then reflect ByWidget, so an ID is passed. All of the calls trace correctly, and the code works in Dev.</p> <p>SetDates that gets called on success in the javascript function is another javascript function, but I have put alerts in SetDates, in the Dev environment the code does not get that far, presumably because it will not find the code in the ValidationController.</p> <p>Any helpful suggestions would be very greatly appreciated!</p> <p><strong>Specs:</strong> MVC3, Javascript 1.6.4, Jquery jquery-ui-1.8.17, IIS 7.5, Umbraco 4.7.1, Visual Studio 2010</p> <p><strong>Javascript code in separate .js file:</strong></p> <pre><code>function getDisabledDatesByWidget(urlGetDisabledDatesByWidget) { alert('urlGetDisabledDatesByWidget = ' + urlGetDisabledDatesByWidget); $.ajax({ //data: {id: id}, type: "POST", async: false, url: urlGetDisabledDatesByWidget, dataType: 'json', success: setDates, constrainInput: true, defaultDate: new Date(defaultCutoff + " 00:00") }); } </code></pre> <p><strong>MVC code in the view, in the document ready function:</strong></p> <pre><code> widgetId = '@ViewBag.WidgetId'; var urlGetDisabledDatesByWidget = '@Url.Action("getDisabledDates", "Validation", new{id = @ViewBag.WidgetId})'; getDisabledDatesByWidget(urlGetDisabledDatesByWidget); </code></pre> <p><strong>MVC code in ValidationController:</strong></p> <pre><code>[HttpPost] public JsonResult getDisabledDates(int id) { List&lt;String&gt; disabledDateStrings = new List&lt;String&gt;(); List&lt;DateTime&gt; _DisabledDateDates = DisabledDateSetup.GetDisabledDateDatesByWidget(id).Value.ToList(); ////Convert disabled dates to string array foreach (DateTime disabledDate in _DisabledDateDates) { disabledDateStrings.Add(disabledDate.ToString("M-d-yyyy")); } ChangeType _changeType = ChangeType.NoChangeType; ChangeDateRangeSetupResponse _dates = DisabledDateSetup.GetValidChangeDateRange(_changeType); return Json(new { disabledDates = disabledDateStrings, startDate = UnixTicks(_dates.Value.BeginDate), endDate = UnixTicks(_dates.Value.EndDate) }); } </code></pre> <p><strong>MVC code in the view, in the div:</strong></p> <pre><code> @Html.TextBoxFor(model =&gt; model.SetupDate, new { id = "DisabledDateCalendar"}) </code></pre> <p><strong>Global asax routing:</strong></p> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Account", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } </code></pre> <p><strong>What I've done to try to resolve this before posting:</strong></p> <ul> <li>Consulted with other developers at my office</li> <li>Traced the calls in Dev using Firefox/Firebug and Chrome/Chrome's F12 feature</li> <li>Placed alerts/messageboxes in code, deployed it, and watched what was displayed and what was not</li> <li>Googled for answers</li> <li>Reviewed many Stack Overflow questions, including:</li> <li><a href="https://stackoverflow.com/questions/6700959/asp-net-mvc-routing-javascript-url-404-errors">asp.net MVC routing - javascript url 404 errors</a></li> <li><a href="https://stackoverflow.com/questions/7864207/handling-asp-net-mvc-routing-in-external-javascript">Handling ASP.NET MVC Routing in External JavaScript</a></li> <li><a href="https://stackoverflow.com/questions/1470613/asp-net-mvc-routing-fails-when-using-default-aspx-controller-action-style-url">ASP.NET MVC routing fails when using default.aspx/controller/action style url</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload