sarpay
2/6/2019 - 1:16 PM

Html Helpers

#C #MVC #Razor Html Helper

Html.Raw

Unencoded Expression

<span>@Html.Raw(model.Message)</span>

Html.Script

@using Health.Web.Web.Framework

// PARENT PAGE
@section PageScriptContent {
  @Html.RenderScripts()
}

// MODAL PARENT PAGE
</script>
....
</script>
@Html.RenderScripts()
 
// PARTIAL OR EDITOR (CHILD)
@Html.Script(
  @<script>
    $(document).ready(function () {
      
    });
  </script>
)

Html.Action

  1. Use when you actually need to retrieve additional data from the server to populate the partial view.
  2. Call a Controller Action, which may return a view/partial view (or may not, it could throw an HttpNotFound or return Json, or other things).
@Html.Action("DocumentListMenu", "Common", new { Area = "" })


Html.Partial

  1. Use when you are rendering static content.
  2. Render a Partial View without hitting a controller action first.
  3. Renders the partial view as an HTML-encoded string.
  4. This method result can be stored in a variable, since it returns string type value.
  5. Simple to use and no need to create any action.
  6. Like RenderPartial method, Partial method is also useful when the displaying data in the partial view is already in the corresponding view model. For example: In a blog to show comments of an article, you can use Partial method since an article information with comments are already populated in the view model. @Html.Partial("_Comments")
public static MvcHtmlString Partial(
  this HtmlHelper htmlHelper, 
  string partialViewName, 
  object model, 
  ViewDataDictionary viewData)

SAMPLE 1

@if (Model.Insureds != null)
{
  for (int i = 0; i < Model.Insureds.Count; i++)
  {
    var viewDataDict = new ViewDataDictionary
    {
      new KeyValuePair<string, object>("Prefix", "Insureds[" + i + "]"),
      new KeyValuePair<string, object>("EndorsementType", Model.EndorsementType.ToString()),
      new KeyValuePair<string, object>("InsuredId", Model.Insureds[i].Id)
    };

    @Html.Partial("_Insured", Model.Insureds[i], viewDataDict)
  }
}

SAMPLE 2

var insuredTemplate = '@Html.Partial("_Insured",
                                      new PolicyInsuredViewModel(),
                                      new ViewDataDictionary()
                                      {
                                        { "DFormIsClosed", Model.Product != null && Model.Product.DFormIsClosed },
                                        { "ProductId", Model.ProductId }
                                      }).ToString().NormalizeHtmlResponse()';

var $insuredRow = $('<div>' +  insuredTemplate + '</div>');

public static MvcHtmlString NormalizeHtmlResponse(this string partialHtml)
{
  if (String.IsNullOrEmpty(partialHtml))
  {
      return MvcHtmlString.Create(partialHtml);
  }
  string lineSeparator = ((char)0x2028).ToString();
  string paragraphSeparator = ((char)0x2029).ToString();
  return MvcHtmlString.Create(
    partialHtml
      .Replace("\r\n", string.Empty)
      .Replace("\n", string.Empty)
      .Replace("\r", string.Empty)
      .Replace(lineSeparator, string.Empty)
      .Replace(paragraphSeparator, string.Empty)
      .Replace("'", "\'")
      .Replace("\"", "\"")
  );
}

Html.RenderPartial

  1. This method result will be directly written to the HTTP response stream means it used the same TextWriter object as used in the current webpage/template.
  2. This method returns void.
  3. Simple to use and no need to create any action.
  4. RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model.For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model. @{Html.RenderPartial("_Comments");}
  5. This method is faster than Partial method since its result is directly written to the response stream which makes it fast.

SAMPLE 1

<tbody>
@if (Model.Targets != null && Model.Targets.Count > 0)
{
  foreach (var target in Model.Targets.OrderBy(model => model.TargetStart))
  {
    Html.RenderPartial("_AgentCommissionProtocolTarget", target);
  }
}
</tbody>