locked
@Url.Content not parsing with html content to partial view RRS feed

  • Question

  • User-1339639702 posted

    Hi Sir,

    I am running one MVC application where in footer i have some link. When user click on the link then i have some html content ready in html page. We are accessing those html in action method and passing to the partial view like below:

     public ActionResult ContactUs()
            {
                var readContact = System.IO.File.ReadAllText(Server.MapPath(Url.Content("~/Content/healthec/contact_us.html")));
                return PartialView("~/Views/Shared/_FooterHtmlLinks.cshtml", readContact);
            }

    and in _FooterHtmlLinks.cshtml parial view i used @Html.Raw(model) to load the html content.

    But in this html i have some images which src value i used like @Url.Content but this is not getting translated by partial view and images is not displaying.

    How could i pass correctly to make it work?

    Tuesday, September 17, 2019 10:16 AM

Answers

  • User-1339639702 posted

    Hi All,

    I found the solution for myself only as i was trying to render html page in razor view using @Html.Raw where i had issue with @Url.Content which was not getting rendered as html.

    To fix this issue in View i used Viewbag having some value to identify if this text present then inside that i am loading that content where already i have @Url.Content used.

    and this viewbag value i set in action method where this partial view is getting called.

    So let's say i have three type of html content then for load i have three action method there i set viewbag value and check the same in partial view once loaded, Once it will match whichever will load those content only.

    Thank you for everyone support on it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 18, 2019 7:51 AM

All replies

  • User475983607 posted

    The standard MVC pattern is using a partial view to display reusable HTML.  It does not make sense to read an HTML file and pass the file as a string to the View. The standard pattern is passing a Model to a View or a Partial to generate dynamic HTML.

    Rather than asking for a specific solution, explain the problem you are trying to solve at a high level.

    Tuesday, September 17, 2019 11:39 AM
  • User-1339639702 posted

    Hi Mgebhard,

    I have one question here like i have to reuse the partial view which load the content. How could i use it.

    Or can we load dynamic html to partial view which have @Url.Content present.

    E.g: If suppose i have some html content in database and from there i have to load it to partial view how do i do it?

    Please suggest.

    Tuesday, September 17, 2019 11:45 AM
  • User475983607 posted

    Vikash.Prasad

    E.g: If suppose i have some html content in database and from there i have to load it to partial view how do i do it?

    This is a standard MVC pattern.  Execute a query that fetches the the data from a table.  Populate a ViewModel from the query result set.  Pass the ViewModel to the View.  Design the View to display the View Model.

    Use @Html.Raw() in the View to render HTML without encoding the HTML.  You must trust the HTML is not malicious.  Generally, this is not a good design approach.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.htmlhelper.raw?view=aspnet-mvc-5.2

    Keep in mind that you have not posted enough code to find any bugs in the design.  I'm assuming the HTML does not contain Razor syntax.

    Tuesday, September 17, 2019 12:01 PM
  • User-1339639702 posted

    Hi All,

    I found the solution for myself only as i was trying to render html page in razor view using @Html.Raw where i had issue with @Url.Content which was not getting rendered as html.

    To fix this issue in View i used Viewbag having some value to identify if this text present then inside that i am loading that content where already i have @Url.Content used.

    and this viewbag value i set in action method where this partial view is getting called.

    So let's say i have three type of html content then for load i have three action method there i set viewbag value and check the same in partial view once loaded, Once it will match whichever will load those content only.

    Thank you for everyone support on it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 18, 2019 7:51 AM