简体   繁体   中英

Can I have variable in HTML.PartialRender()?

我可以吃点什么吗?

 @{HTML.PartialRender(variable);} // where variable will be a path of a file

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

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. 

Use overload: RenderPartial(HtmlHelper, String, Object)

Eg.

@{Html.RenderPartial("PartialViewName", new { filePath = model.FilePath});}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM