简体   繁体   English

MVC视图只能支持两种操作:一种是httpget和httppost?

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

I have 2 views say view1 and view2 . 我有2个视图,分别是view1view2 In view1 there is a link for view2 named "Create new Employee". view1有一个名为“创建新员工”的view2链接。 When user click on it, view2 will show. 当用户单击它时,将显示view2 View2 contains a form in which employee fill its basic details. View2包含一个表格,员工可以在其中填写其基本详细信息。 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. 如果员工单击它,则view2再次显示填充的数据和用户编辑,然后单击保存并将数据保存在数据库中。 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. 1)显示view2 :-一种正常的操作方法,显示具有空表格的view2并由员工填写。

2) Filling view2 : - An [HttpPost] action method which post the form 2)填充view2 :-发布表单的[HttpPost]操作方法

3) Edit view2 - Edit view2 which is populated and save. 3)编辑view2编辑已填充并保存的view2

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. 我们只有两种方法来编写动作方法:一种是httpget,另一种是httppost,最终每次只能完成两种情况。

thanks in advance!! 提前致谢!!

You will use [HttpPost] both when creating and when saving a user. 创建和保存用户时都将使用[HttpPost] 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). 实际上,这可能是处理两种情况的相同方法:创建时,表单中的隐藏Id字段将为空,而编辑同一字段时将具有一个值(在创建时分配Id )。

Your method could use the value of this field to decide what to do. 您的方法可以使用该字段的值来决定要做什么。

You can use HttpPut for edit/update. 您可以使用HttpPut进行编辑/更新。

This matrix is a good representation on REST and the verbs used for each CRUD action. 矩阵很好地表示了REST和每个CRUD动作所使用的动词。

Now by default, ASP.NET MVC3 does not support Put Form method. 现在默认情况下,ASP.NET MVC3不支持Put Form方法。 You can use MVCContrib 's SimplyRestfulRouteHandler for this purpose. SimplyRestfulRouteHandler ,可以使用MVCContribSimplyRestfulRouteHandler

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. 我只需要一个View2的视图模型,其中包括一个模式变量,该变量可以是一个布尔值或一个枚举,以表示该视图是处于编辑模式还是显示模式。

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. 然后,在action方法的HttpGet版本中,填充视图模型对象,然后将此视图模型对象提供给视图以进行呈现。

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. 然后,视图可以使用给定的信息来呈现各种输入或其他HTML元素,并使用上述的布尔值或枚举值来决定是在显示模式下还是在编辑模式下呈现视图。

The HttpPost method remains the same. HttpPost方法保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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