+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / MVC Ajax: Url.Action renders wrong url

MVC Ajax: Url.Action renders wrong url

16 posts, 0 answered
  1. Christian
    Christian avatar
    5 posts
    Registered:
    13 Sep 2013
    24 Sep 2013
    Link to this post
    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

  2. David C
    David C avatar
    33 posts
    Registered:
    02 Dec 2015
    25 Sep 2013
    Link to this post
    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
  3. Christian
    Christian avatar
    5 posts
    Registered:
    13 Sep 2013
    26 Sep 2013 in reply to David C
    Link to this post
    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

  4. Natalya
    Natalya avatar
    4 posts
    Registered:
    04 Oct 2013
    17 Oct 2013 in reply to Christian
    Link to this post
    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!
  5. Christian
    Christian avatar
    5 posts
    Registered:
    13 Sep 2013
    17 Oct 2013 in reply to Natalya
    Link to this post
    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
  6. Natalya
    Natalya avatar
    4 posts
    Registered:
    04 Oct 2013
    17 Oct 2013 in reply to Christian
    Link to this post
    Thanks for fast replay, Christian!
  7. Gary
    Gary avatar
    7 posts
    Registered:
    27 Mar 2014
    08 Apr 2014
    Link to this post

    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?

  8. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    23 Sep 2016
    11 Apr 2014
    Link to this post
    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
     
  9. Tejal
    Tejal avatar
    7 posts
    Registered:
    26 Apr 2014
    04 Jun 2014
    Link to this post

    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

  10. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    23 Sep 2016
    16 Jun 2014
    Link to this post
    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
     
  11. Tejal
    Tejal avatar
    7 posts
    Registered:
    26 Apr 2014
    17 Jun 2014 in reply to Nikola Zagorchev
    Link to this post

    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

     

     

  12. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    23 Sep 2016
    17 Jun 2014
    Link to this post
    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
     
  13. Celso
    Celso avatar
    6 posts
    Registered:
    03 Jul 2015
    29 Oct 2015
    Link to this post

    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());

    }

  14. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    23 Sep 2016
    29 Oct 2015
    Link to this post
    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
     
  15. Celso
    Celso avatar
    6 posts
    Registered:
    03 Jul 2015
    29 Oct 2015
    Link to this post

    Hello Nikola,

     

    I eventually figured that out. Now it would be great if it actually worked, but I can't seem to make VS recognize the extension method..

  16. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    23 Sep 2016
    29 Oct 2015
    Link to this post
    Hi Celso,

    Please, verify you have a using in the view for the namespace of the class that holds the extension methods.

    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
     
16 posts, 0 answered