且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在mvc中动态显示模型的属性?

更新时间:2023-12-02 19:11:16

创建两个部分视图,一个返回FruitModel的类型,其他类型为StudentModel并且在主视图中你不需要指定ViewModel类。在ActionResult中将ViewData或ViewBag中的状态设置为fruitModel或StudentModel。

Create two partial views one to return the type of FruitModel and other be the type of StudentModel and in the main view you no need to specify a ViewModel class.In the ActionResult set a status in ViewData or ViewBag as fruitModel or StudentModel.
public ActionResult Action()
{
if(it's studentModel)
   ViewData["Status"]=1
else
 ViewData["Status"]=2
return View();
}





之后从视图中可以返回基于viewData值的局部视图,如下所示。



After that from the view you can return the partial view based on the viewData value like below.

@if(Viewdata["Status"]==2)
Html.RenderPartial( "FirstPartialView")
else
Html.RenderPartial( "SecondPartialView")



希望这有助于


Hope this helps