ASP.NET MVC 4 Partial View templates - invalid template issue.

2 posts, 0 answers
  1. Phil
    Phil avatar
    4 posts
    Member since:
    Jun 2011

    Posted 07 Sep 2012 Link to this post

    I am wondering if there is any sample code / API that can process the output after calling HtmlHelper.Partial(...).  In many cases, we use the same partial view to render the repeating items and also the template for when the user adds new items at the client.  The '#' character is the trouble.  In many cases a # character is used in an <a href='#'...  or else where which is not part of the template. Recently, I just used the following code to get around the href='#' issue,

    var template = html.Partial(...);
    string str = template.ToHtmlString();

    str = str.Replace("href=\"#\"", "href=\"\\\\#\"");
    str = str.Replace("href='#'", "href='\\\\#'");

    return new MvcHtmlString(str);

    it would be great if someone had a regex or a helper function that could escape all the non-template '#' characters.  I would really be unfortunate if I have to duplicate all my partial views just make them template safe.

  2. Phil
    Phil avatar
    4 posts
    Member since:
    Jun 2011

    Posted 08 Sep 2012 Link to this post

    After sleeping on this question, I thought it might be better for the partial views to be 'template' aware.  I can add a special marker item to the ViewDataDictionary which identifies the partial is being rendered as a template.  The partial view can then escape the characters correctly.  I think this will be less error prone than trying to parse/search/replace the rendered partial result.
  3. Kendo UI is VS 2017 Ready
Back to Top