How to create reusable control in ASP.NET MVC

后端 未结 4 1282
有刺的猬
有刺的猬 2021-02-03 12:42

How can/should I create some \"custom control\" in ASP.NET MVC 3? I have red about partial views, ViewUsersControl, Html.RenderAction, but I still don\

4条回答
  •  爱一瞬间的悲伤
    2021-02-03 13:18

    You may use

    @{Html.RenderPartial("YourCustomView",YourModel);}
    

    For instance, In your Shared folder create a new View and name it "_MyCustomControl"

    Then in the code write :

    @model YourNameSpace.Models.YourModel
    
    @{
        Layout = null;
    }
    
    @*Here write your control's markup*@
    

    Then in your Views where you want to use this "control" you add:

    @{Html.RenderPartial("_MyCustomControl",new YourModel { args });}
    

    If you get into trouble with RenderPartial check this out

提交回复
热议问题