0

I am trying to construct a URL using a HTML helper extension method while trying pass in parameters to the extension method. For example

    public static MvcHtmlString GenerateActionLink(this HtmlHelper html,string displayText,string id,int logicstatusId)
    {
       var actionName = string.Empty;
       var controllerName = string.Empty;

       if (logicstatusid == 5)
       {
         actionName = "Basic";
         controllerName = "HighBasic";  
       }
       else
       {
         action = "Advanced";
         controllerName = "HighAdvanced";
       }

        var targetURL = UrlHelper.GenerateUrl("Default", action, controller,     new RouteValueDictionary(new { id = id}), RouteTable.Routes,   html.ViewContext.RequestContext, false);

        //Create the hyper link tag
        var anchorLinkBuilder = new TagBuilder("a");
        //Merge the target URL with the href attribute
        anchorLinkBuilder.MergeAttribute("href", targetURL);
        return MvcHtmlString.Create(anchorLinkBuilder.ToString(TagRenderMode.Normal));
    }

While this helper method is working, the problem I am facing is on the client side.

      var cellHtml = '<div class="action-column">';
      var id= row.encryptedId;
      cellHtml += '@Html.GenerateHtmlLink("Blip","'+ id+'" , 4)';
      cellHtml += "</div>";
      return cellHtml;

In this case the URL is getting constructed but the id parameter is not passing on to the helper method. I am not sure if I have done the passing of the parameter the right way. I'd appreciate if anybody help out.

3
  • 1
    Your C# code (call to the method) gets executed in server. At that time the js variable value will not be there. If you absolutely need to get this user, expose the method via an action method and your js code can make an ajax call to get this url generated by your method. Commented Sep 1, 2016 at 2:31
  • You are saying create an action method to build the URL and call the action via js and get the generated URL ? Commented Sep 1, 2016 at 2:38
  • Yes. If you need to get this in javascript ( by passing some js variable value) Commented Sep 1, 2016 at 2:39

1 Answer 1

2

Your C# code (call to the GenerateActionLink helper method) gets executed in server when razor tries to render the view. At that time the js variable value will not be there. The output of razor executing all the C# code view file is just the html markup which the browser will render. Only after that your javascript will be executed and the js variable value will be avaialble.

If you absolutely need to generate the dynamic url (for each id/logicstatusId value) in your client side javascript code using the UrlHelper method, you might consider exposing that C# code via an action method. Whenever you need the link url in your javascript code, make an ajax call to the action method, pass the parameter value and get the url.

public string GenerateActionLink(string id, int logicstatusId)
{
    var actionName = "Advanced";
    var controllerName = "HighAdvanced";

    if (logicstatusId == 5)
    {
        actionName = "Basic";
        controllerName = "HighBasic";
    }

    var targetUrl = UrlHelper.GenerateUrl("Default", actionName, controllerName, new RouteValueDictionary(new { id = id }), RouteTable.Routes, Request.RequestContext, false);
    return targetUrl;            
}

And in client side

var id = 1;
$.get('/Home/GenerateActionLink?logicstatusId=5&id=' + id,function(res) {
    var htmlMarkup = '<a href="' + res + '">Blip</a>';
    // do something with htmlMarkup
    // Ex : $('#SomeDivId').append(htmlMarkup);
});

But if you want to do this for many items, you might not want to make a call for each items, In that case,I would generate the base links and conditionally append the querystring values in javascript

var baseUrlBasic = "@Url.Action("Basic","HighBasic");
// Now later
var id = 1;
var newUrl = baseUrl+'?logicstatusId=5&id='+id;
// Use this to build the anchor tag
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the prompt answer. I ended getting the flags marked in the database and then used the flag to construct the link in the view itself. However I am marking your answer as the THE answer because it did help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.