This is a migrated thread and some comments may be shown as answers.

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

1 Answer 162 Views
Templates
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 07 Sep 2012, 11:22 PM
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.

1 Answer, 1 is accepted

Sort by
0
Phil
Top achievements
Rank 1
answered on 08 Sep 2012, 04:07 PM
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.
Tags
Templates
Asked by
Phil
Top achievements
Rank 1
Answers by
Phil
Top achievements
Rank 1
Share this question
or