简体   繁体   English

在ASP.NET MVC中处理2种不同形式之间的共享数据时,最佳实践是什么?

[英]What is the best practice when handling shared data between 2 different forms in asp.net mvc?

Suppose you have an mvc razor webpage in which the user can approve or reject a request of some sorts but he would also have to include a reason. 假设您有一个mvc剃刀网页,用户可以在其中批准或拒绝某种请求,但他还必须提供原因。

Now, you could have 2 input fields: the first is to specify a reason for approving the request; 现在,您可以有2个输入字段:第一个是指定批准请求的原因;第二个是输入请求的原因。 the second is to specify a reason for rejecting the request. 第二个是指定拒绝请求的原因。 In this case all will be fine and dandy. 在这种情况下,一切都会好起来的。

What if you, as a UI designer, thought that just one field for specifying the reason would be enough? 作为UI设计人员,如果您认为仅一个字段指定原因就足够了? In this case you would have two submit buttons or inputs (one for approving and one for rejecting) and in order to post to the appropriate controller you would have to have 2 forms for each of those submit buttons but you cannot share the 'reason' input field between the 2 forms. 在这种情况下,您将有两个提交按钮或输入(一个用于批准,一个用于拒绝),并且要发布到适当的控制者,您必须为每个提交按钮具有2个表单,但不能共享“原因” 2种形式之间的输入字段。

So what would be the best practice for such a scenario? 那么,在这种情况下的最佳实践是什么?

PS: I know there are solutions to this: using ajax or other javascript to perform the post, defining and using attributes to pair controllers with their respective buttons, using the formaction html 5 attribute and perhaps other solutions I missed but they either seem gimmicky or have certain downsides. PS:我知道有解决方案:使用ajax或其他javascript执行发布,定义并使用属性将控制器与其各自的按钮配对,使用formaction html 5属性以及我可能错过的其他解决方案,但是它们看起来花哨或有某些缺点。

Why do you need two forms? 为什么需要两种形式? You can simply do something like the following: 您可以简单地执行以下操作:

<form action="" method="post">
    <textarea name="reason"></textarea>
    <button type="submit" name="approve" value="true">Approve</button>
    <button type="submit" name="approve" value="false">Reject</button>
</form>

The buttons then operate similarly to a radio. 然后,按钮的操作类似于收音机。 Whichever one the user clicks, that is the value that will be posted. 用户单击的任何一个,即将被发布的值。 Then, you can simply branch on that boolean and either approve or reject accordingly. 然后,您只需在该布尔值上分支,然后相应地批准或拒绝即可。

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

相关问题 在Asp.Net MVC 5中实现多个数据库的最佳实践是什么? - What is the best practice for implementing multiple databases in Asp.Net MVC 5? ASP.NET MVC最佳实践:传递数据 - ASP.NET MVC Best Practice: Passing data 在asp.net中输入一堆数据的最佳实践是什么? - What is the best practice to input a bunch of data in asp.net? 在asp.net中缓存用户数据的最佳实践是什么 - What is the best practice for caching user data in asp.net 在ASP.NET MVC中为不同用户加载不同主题或CSS文件的最佳实践 - Best practice for loading different themes or css files for different users in ASP.NET MVC 在ASP.NET MVC中显示数据的最佳解决方案是什么? - What is the best solution for displaying data in ASP.NET MVC? ASP.net 默认资源文件处理 - 最佳实践? - ASP.net default resource file handling - best practice? 使用ASP.NET Web API进行错误处理的最佳实践 - Best practice for error handling with ASP.NET Web API 使用ASP.NET MVC呈现大量html或文本文件的最佳实践是什么? - What is the best practice for using ASP.NET MVC to render lots of html or text files? 如何在ASP.NET MVC中通过Ajax为表添加排序功能?最佳实践是什么 - How to add sort function for the table via ajax in ASP.NET MVC?What is the best practice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM