2

Can I have Something like:

 @{HTML.PartialRender(variable);} // where variable will be a path of a file
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1778595
  • 29
  • 1
  • 3

3 Answers3

1

Yes, you can use a variable as the path parameter when the view is being rendered on the server.

@{
    string path = "foo/bar"; // a path which the view engine can locate
}

<div>
    @{ Html.RenderPartial( path ); }

    @* OR *@

    @Html.Partial( path )
</div>

Since the question is also tagged with JavaScript, I will point out that you can't mix Razor (server) rendering with client (JavaScript) execution. However, you can easily invoke a controller using AJAX (and pass it whatever data you want) and that controller can return a rendered view.

See also: Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • So, I can't use Javascript function, to return a string path to a Razor, right? That, also mean that I can't use a "path" instance variable, where I will be getting a path from JavaScript. – user1778595 Feb 11 '13 at 14:51
  • Correct. However, you could pass your JavaScript variable to a controller with an AJAX request and have the controller return you a different/customized view. – Tim M. Feb 11 '13 at 14:57
  • You should update your question with the code you tried (or open another question). – Tim M. Feb 11 '13 at 17:02
0

These are Overload for RenderPartial

     1.RenderPartial(HtmlHelper, String)

Renders the specified partial view by using the specified HTML helper.

     2. RenderPartial(HtmlHelper, String, Object)   

Renders the specified partial view, passing it a copy of the current ViewDataDictionary object, but with the Model property set to the specified model.

     3. RenderPartial(HtmlHelper, String, ViewDataDictionary)   

Renders the specified partial view, replacing its ViewData property with the specified ViewDataDictionary object.

     4. RenderPartial(HtmlHelper, String, Object,
     ViewDataDictionary)  Renders the specified partial view, replacing
            the partial view's ViewData property with the specified
            ViewDataDictionary object and setting the Model property of the view
            data to the specified model.
0

Use overload: RenderPartial(HtmlHelper, String, Object)

Eg.

@{Html.RenderPartial("PartialViewName", new { filePath = model.FilePath});}
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52