MVC Ajax: Url.Action renders wrong url - Bugs & Issues - Bugs & Issues - Progress Community
 Bugs & Issues

MVC Ajax: Url.Action renders wrong url

  • MVC Ajax: Url.Action renders wrong url
  • Hi,
    I see, that this issue was addressed several times before but not resolved.

    I created a route for ajax calls with a url prefix like the developer documentation suggests.

    Telerik.Sitefinity.Abstractions.Bootstrapper.MVC.MapRoute(
                   "AjaxCall", // Route name
                   "ajax/controller/action/id", //prefix
                   new controller = "Hausrat", action = "Index", id = UrlParameter.Optional
               );

    The action, which is called via ajax returns HTML(partial view).
    Every @Url.Action in this partial view renders this URL: ...Sitefinity/Themes?action=ActionName&controller=Controllername.

    Is this a bug or did I forget something?

    Regards,

    Christian

  • Hi Christian,

    Can you please provide me with a link to the documentation that you are following? I want to make sure we're on the same page.

    Also, there is a thread that is currently active that might be of some interest to you:

    MVC JSON ActionResult returns all page HTML output

    Take a look at the entire thread as there's additional info down at the bottom and let us know if this sheds any light on to your situation.

    Regards,
    David C
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • Hi David,

    I got the info from yout videos on youtube(Problem with JSON returns full page).
    So i did the same for an ajax action, which returns a partial view.
    Everything works fine.
    But the helpers behave strangly:
    @Url.Action("SaveErgebnisSelection", "Hausrat", new selectedTarifId = Model[cnt].TarifID )
    Expected Link:
    /Hausrat/SaveErgebnisSelection?selectedTarifId=00000-0000-00000
    Actual generated Link:
    /Sitefinity/Themes?action=SaveErgebnisSelection&controller=Hausrat

    (It's a german site so controller and action names are in german)

    So i tried to createthe link manually but:
      HttpContext.Current.Request.RequestContext.RouteData.Values["controller"]
    is null.

    What am I misssing and where does sitefinity store the controller name?

    Regards,

    Christian

  • Christian, have you found a way to solve this problem? If yes - could you please share it with me? I've run into the same issue just now and have no ideas. Thanks!
  • Hi Natalya,

    it is a known issue with Sitfinity.
    My solution was to write my own Html.Action helper:
    In simple form:
    using System;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
     
       
    namespace SitefinityWebApp
        public static class HelperExtensions
        
            public static MvcHtmlString ActionLink1(this HtmlHelper helper, string linkText, string actionName, string controllerName, string actionPrefix)
            
                StringBuilder sb = new StringBuilder();
                sb.Append("<a href=\"/");
                sb.Append(actionPrefix);
                sb.Append("/");
                sb.Append(controllerName);
                sb.Append("/");
                sb.Append(actionName);
                sb.Append("\">");
                sb.Append(linkText);
                sb.Append("</a>");
       
                return new MvcHtmlString(sb.ToString());
            
        
    Then it will be used like so:
    @Html.ActionLink1("TestLink", "TargetAction", "Test", "customprefix");

    Regards,

    Christian
  • Thanks for fast replay, Christian!
  • I seem to be having this issue as well if I try to use Url.Action in a regular MVC view routed in "classic" mode using Bootstrapper.Mvc.MapRoute.

     Any updates on this that don't require working around standard MVC calls?

  • Hello,

    Unfortunately, you should still use the helper or go with this workaround of defining the actual link:
    @Html.ActionLink("MyLink", "Index", null, new href = "MyController/Index")

    You can use the same approach for action of form, as well:
    @using (@Html.BeginForm("Test", "MyMvc", FormMethod.Post, new action = "/MyMvc/Test?myvalue=" + Model.Title))
        
            <div>@Html.ValidationSummary()</div>
           @Html.LabelFor(s=> s.Title)
    @Html.TextBoxFor(s=> s.Title)
     
            <button type="submit" class="button" title="Go">Go</button>
        
    I hope this helps.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  • Hi,

     I developed one custom widget in MVC.Inthat I used ajax request to get the list of records to bind to dropdown With jQuery. This is an easy task, but I am not able to return the json data to the client. I returned a JsonResult on the Controller but Sitefinity always injected somehow the master layout of the page and  therefore it was invalid json when it was received by the client.

    Here is my ajax call:

     $.ajax(
                url: "/careers/Register.aspx/getJobPostings",
                dataType: "json",
                data: ,
                contentType: "application/json;charset=utf-8",
                accepts: "application/json",
                type: "GET",
                error: function (xhr)
                    alert("Something seems Wrong" + xhr);
                ,
                success: function (data)
                    if (data)
                        alert(data);
                        $.map(data, function (item)
                            $('<option value="' + item.Department + '">' + item.Department + '</option>').appendTo("#ddlDepartment");
                        );
                   
               
            );

     

    And here is the code to return JSON data

     public JsonResult getJobPostings()
           
                
                var myCollection = RetrieveCollectionOfJobPostings();
                List<JobPosting> listJobPosting = new List<JobPosting>();
                foreach (var obj in myCollection)
               
                    objJobPosting = new JobPosting();
                    objJobPosting.Title = obj.GetValue("Title").ToString();
                    objJobPosting.Department = obj.GetValue("Department").ToString();
                    objJobPosting.Description = obj.GetValue("Description").ToString();
                    objJobPosting.Company = obj.GetValue("Company").ToString();
                    objJobPosting.NumberOfVacancies = decimal.Parse(obj.GetValue("NumberOfVacancies").ToString());
                    objJobPosting.EmailId = obj.GetValue("EmailId").ToString();
                    listJobPosting.Add(objJobPosting);
               

                var jsonData = (from item in listJobPosting
                                select new
                               

                                    Title = item.Title,
                                    Department = item.Department,
                                    Description = item.Department,
                                    Company = item.Company,
                                    NumberOfVacancies = item.NumberOfVacancies,
                                    EmailId = item.EmailId

                                );

               
                return Json(jsonData, JsonRequestBehavior.AllowGet);
                
           

    Any solutions??

     

    Thanks,

    Tejal

  • Hi Tejal,

    This is a known issue when using MVC widgets. There is a bug logged, which you can find and vote for here. There are a number of workarounds suggested in this forum thread. One of them is to use a MVC Controller in Classic mode and call the JSON result Actions from it. Another solutions could be found in the above mentioned thread.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  • Hello Tejal,

    I am glad you have resolved the issue.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  • Hi Nikola,

     Thanks for reply. I found the solution as you mentioned,is to use a MVC Controller in Classic mode and call the JSON result Actions from it. It worked for me.

    Regards,

    Tejal Satre

     

     

  • I'm using Christian's approach, I've used it to also inject Html content into the link but there's something which I don't understand..

    When we're passing "this UrlHelper helper" on the parameters list, what should I be passing here? From the example it seems like nothing is passed here but on VS I get an error if I don't pass a URL, even though it's not used in the method body itself.

     Could anyone help me out here?

    Here's my method:

    public static MvcHtmlString SyActionLinkHTML(MvcHtmlString htmlContent, string actionName, string controllerName, string actionPrefix, string param = "")

     

    StringBuilder sb = new StringBuilder();     

    sb.Append("<a href=\"/");     

    sb.Append(actionPrefix);     

    sb.Append("/");     

    sb.Append(controllerName);     

    sb.Append("/");     

    sb.Append(actionName);     

    sb.Append("/");     

    sb.Append(param);     

    sb.Append("\">");     

    sb.Append(htmlContent);     

    sb.Append("</a>");  

    return new MvcHtmlString(sb.ToString());

  • Hi Celso,

    The "this HtmlHelper helper" specifies that this is an extension method of the Html helper and in a razor view you will call your extension method through the HtmlHelper:
    @Html.MyExtensionMethod

    the same is for "this UrlHelper helper" but for the UrlHelper:
    @Url.MyExtensionMethod


    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items