Access Parent Model from partial view

前端 未结 5 2040
半阙折子戏
半阙折子戏 2021-01-07 22:00

I\'m asking because the partial view I will create is blank, with the purpose of creating a new child entity. I just need a quick, regardless if dirty, way to access the Pa

5条回答
  •  粉色の甜心
    2021-01-07 22:42

    I know this is an old topic, but I figured I'd just add my solution to the same problem anyway. I think it's a bit cleaner.

    Basically add a model to the partial view.

    The encapsulating view:

    @model whatever
    ...
    @Html.Partial("partialview", anotherwhatever)
    

    The partial view:

    @model anotherwhatever
    
    @Model.something
    ...

    In my case I just needed to pass a string into the partial view (just using it to shorten and partition code), so this was much more elegant than the other solution.

    I tried the other solution first and actually couldn't get it to work, it just acted as though the value I passed was blank.

提交回复
热议问题