将数据传递到部分视图

text
阅读 33 收藏 0 点赞 0 评论 0

data-to-partial
You simply have some value in your main view that you want to access in a 
partial being rendered in that view.

If you just render a partial with just the partial name:

@Html.Partial("_SomePartial")
It will actually pass your model as an implicit parameter, the same as if you were to call:

@Html.Partial("_SomePartial", Model)
Now, in order for your partial to actually be able to use this, though, 
it too needs to have a defined model, for example:

@model Namespace.To.Your.Model

@Html.Action("MemberProfile", "Member", new { id = Model.Id })
Alternatively, if you're dealing with a value that's not on your view's model 
(it's in the ViewBag or a value generated in the view itself somehow, 
then you can pass a ViewDataDictionary

@Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } });
And then:

@Html.Action("MemberProfile", "Member", new { id = ViewData["id"] })
As with the model, Razor will implicitly pass your partial the view's ViewData by default, 
so if you had ViewBag.Id in your view, then you can reference the same thing in your partial.
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号