简体   繁体   中英

Can MVC view only support two operation: One is httpget and httppost?

I have 2 views say view1 and view2 . In view1 there is a link for view2 named "Create new Employee". When user click on it, view2 will show. View2 contains a form in which employee fill its basic details. After filling the form employee click save and data will save in database. Now View displays all the employees details. There is an edit link after each employee detail. If employee click on it , the view2 again displayed with populated data and user edit and click save and data saved in database. So finally we have three scenarios given below:-

1) Display view2 :-A normal action method which display view2 with empty form and employee fill in it.

2) Filling view2 : - An [HttpPost] action method which post the form

3) Edit view2 - Edit view2 which is populated and save.

My question is how can these three scenarios will occur on save view? We have only two ways to write action method : one is httpget and another is httppost which ultimately only accomplish two scenarios each time.

thanks in advance!!

You will use [HttpPost] both when creating and when saving a user. In fact it could be the same method that handles both cases: when creating, a hidden Id field in the form will be empty and when editing the same field will have a value (the Id is assigned on creation).

Your method could use the value of this field to decide what to do.

You can use HttpPut for edit/update.

This matrix is a good representation on REST and the verbs used for each CRUD action.

Now by default, ASP.NET MVC3 does not support Put Form method. You can use MVCContrib 's SimplyRestfulRouteHandler for this purpose.

I would just have a view model for View2 that included a mode variable, which can either be a boolean or an enum to signify whether the view is in edit mode or display mode.

Then, in the HttpGet version of the action method, populate the view model object and give this view model object to the view to render.

The view can then render the various inputs or other HTML elements with the given information and use the aforementioned boolean or enum value to decide whether to render the view in display mode or edit mode.

The HttpPost method remains the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM