c# - ASP.NET MVC3 Import view content programatically on an extension method -


I am creating a state machine which, according to some dynamic values, different action controls (partial view) Presents.

I started writing HtmlHelper extension methods to use the proper html for each state. Something like this:

  @if (model.state == "NEW") {Html.RenderActionEdit () Html.RenderActionDelete ()}   

and so on I am doing simple ways in the form:

  Back MvcHtmlString.Create ("
.....

But it is quite hard for large pieces of HTML. So, the question is, is it possible to write this HTML on different HTML views and then load them anyway and pass the result onto MvcHtmlString? Like

  return MvcHtmlString.Create (see .load ("EditAction.csthml"));   

I could not find a way to load an existing view and then "include it" on the partial method output.

Many thanks for any help!

There are a few ways to do this:

  1. < Code> @ html.RenderPartial ("thepartial.cshtml", model); will pass model in partial view, and it must be presented. There are some other versions too.

  2. The code will be passed from the specified code method to id , and will output the output to view it. It is very convenient if you do not have the required model object for partial model available in your main view.

    In an extension method on HtmlHelper, you can use it:

      public HtmlString YourContent (HtmlHelper Assistant) {return Assistant. Return ("Action", "Controller", New {id = 1}); }   

    which in your view is @ Html.YourContent () .

Comments