ASP.NET MVC - using the same form to both create and edit

前端 未结 10 896
温柔的废话
温柔的废话 2021-01-30 05:27

Whats the best practice approach to creating a form that is used to both create new models and edit existing models?

Are there any tutorials that people can point me in

10条回答
  •  星月不相逢
    2021-01-30 05:45

    I have a system that I think works pretty well. In my shared views I have 2 generic forms, Edit.aspx and New.aspx

    Then in my specific view folder I have a control named EditItems.ascx

    In my edit form I have the form tags and specific buttons for edit and in the new form I have the form tags and specific buttons for new. In each I have Html.RenderPartial("EditItems.ascx")

    This way your user control can be strongly typed and yet you are reusing the look and feel of the edit and new pages.

    Now in some cases, your new page might have a different layout than the Edit page. In that case just add "Edit.aspx" to your specific view folder.

    I find this gives me the best combination of reuse while still allowing full customization should I need it. And as for controller actions, yes they should be separate actions.

提交回复
热议问题