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

前端 未结 10 828
温柔的废话
温柔的废话 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:54

    I put the form itself in a user control - say, Views/Shared/WidgetForm.ascx. I put all form fields in this user control, but NOT the form tags themselves.

    The views, say Views/Widgets/New.aspx and Views/Widgets/Edit.aspx, have the form tags in them and all the "surroundings" - instructions for filling in the form, page title, etc etc. Then they include the user control inside the form tags.

    The user control simply takes a Widget object, and displays a form based on the results. Putting sensible defaults in new Widget options therefore becomes important, but you're doing that anyway, right? ;)

提交回复
热议问题