C# 如何使用 ASP.Net MVC 制作向导

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/297148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:08:21  来源:igfitidea点击:

how to make a wizard with ASP.Net MVC

c#.netasp.net-mvcwizard

提问by

Our site has multiple "wizards" where various data is collected over several pages, and cannot be committed to the database until the last step.

我们的站点有多个“向导”,其中的各种数据是在多个页面上收集的,并且在最后一步之前无法提交到数据库中。

What is the best/correct way to make a wizard like this with ASP.Net MVC

使用 ASP.Net MVC 制作这样的向导的最佳/正确方法是什么

edit: My boss is now saying "no javascript" - any thoughts on how to get around that restriction?

编辑:我的老板现在说“没有 javascript” - 关于如何绕过这个限制的任何想法?

采纳答案by Joel Martinez

I don't believe there is a best/correct way, but the way I'd do it is...

我不相信有最好/正确的方法,但我这样做的方法是......

Each wizard gets its own page. Each step gets its own div. All steps are in the same form.

每个向导都有自己的页面。每一步都有自己的 div。所有步骤都采用相同的形式。

The previous/next buttons would essentially hide/show the div in each step of the process. The last step's submit button submits the entire form. It would be pretty trivial to implement this using jQuery, and it would be easy to maintain as all the wizard steps are in a single ViewPage.

上一个/下一个按钮基本上会在过程的每个步骤中隐藏/显示 div。最后一步的提交按钮提交整个表单。使用 jQuery 实现这一点非常简单,而且由于所有向导步骤都在单个 ViewPage 中,因此易于维护。

On the controller side, you'd have two controller methods, the HttpVerbs.Get version that would prepare the form for viewing and the HttpVerbs.Post version that would take a FormsResult and parse it to get out the information required to submit the user's answers to storage/other processes.

在控制器方面,您有两个控制器方法,HttpVerbs.Get 版本将准备用于查看的表单和 HttpVerbs.Post 版本将采用 FormsResult 并解析它以获取提交用户答案所需的信息到存储/其他过程。



Wow, your boss stinks.

哇,你的老板很臭。

This answer almost gracefully works for those ****** who have javascript disabled (yes, both of them). You can tweak it to hide the next-previous buttons via CSS and unhide them in your javascript code. This way people with javascript see the wizard and people without javascript will see the entire form (without next/prev buttons).

这个答案几乎适用于那些禁用了 javascript 的 ******(是的,他们两个)。您可以调整它以通过 CSS 隐藏下一个上一个按钮,并在您的 javascript 代码中取消隐藏它们。这样,使用 javascript 的人可以看到向导,而没有 javascript 的人将看到整个表单(没有下一个/上一个按钮)。

The other option is to create a view for each step in the wizard. You can store the intermediate results of the form in the Session. This way would cost plenty of time and effort to implement, which means you could probably squeeze some overtime out of your boss when you demonstrate, in about twenty minutes of effort you spend during lunch, how easy the javascript route is to implement.

另一个选项是为向导中的每个步骤创建一个视图。您可以将表单的中间结果存储在 Session 中。这种方式会花费大量的时间和精力来实施,这意味着当你证明 javascript 路线是多么容易实施时,你可能会从你的老板身上挤出一些加班时间,在你午餐时花费大约 20 分钟的时间。

回答by Joel Martinez

Make the different panels all be client side ... all in the same form ... and when the final submit button is pressed, you can post all of the values at the same time.

使不同的面板都在客户端......全部采用相同的形式......当按下最终提交按钮时,您可以同时发布所有值。

回答by Matthew

If you can't use JavaScript, then make each step a view, with a method in the controller, and keep your data in session until ready to submit to the database.

如果您不能使用 JavaScript,则将每一步都设为视图,并在控制器中使用一个方法,并将您的数据保持在会话中,直到准备好提交到数据库。

You can make your Next and Prev buttons using the ActionLink HtmlHelper method.

您可以使用 ActionLink HtmlHelper 方法制作 Next 和 Prev 按钮。

回答by rodbv

If you can't use Javascript and don't want to spend server resources with Session variables, you can also serialize and deserialize the values being entered in the different steps, and pass it back and forth using a hidden input field. A bit like ViewState in ASP.NET Webforms.

如果您不会使用 Javascript 并且不想将服务器资源用于 Session 变量,您还可以序列化和反序列化不同步骤中输入的值,并使用隐藏的输入字段来回传递它。有点像 ASP.NET Webforms 中的 ViewState。

回答by Ben Mills

Another way is to save the incomplete object that you are building with the wizard to the database and just pass the primary key to the next step of the wizard. I know that this means you need to make some of the database fields nullable, but it does have the added advantage that you can save the primary key in a cookie and allow the user to come back to the wizard at some later time. This option doesn't require javascript or session state.

另一种方法是将您使用向导构建的不完整对象保存到数据库中,然后将主键传递给向导的下一步。我知道这意味着您需要将某些数据库字段设为可空,但它确实具有额外的优势,您可以将主键保存在 cookie 中,并允许用户稍后返回向导。此选项不需要 javascript 或会话状态。

回答by Ben Mills

I put together a login wizard and documented the ideas behind it on my blog if that helps: link text

我整理了一个登录向导,并在我的博客上记录了它背后的想法,如果有帮助的话:链接文本

回答by Max Zerbini

You can use the simple component MVCWizard.Wizard available on NuGet. The WizardController allows you to create a wizard using partial view. There is also the AutoWizardController that renders the entire wizard in a single view. All these components operate with the session to store the model state.

您可以使用 NuGet 上提供的简单组件 MVCWizard.Wizard。WizardController 允许您使用局部视图创建向导。还有 AutoWizardController 可以在单个视图中呈现整个向导。所有这些组件都与会话一起运行以存储模型状态。

回答by soflexible557

There is a very simple, flexible and extensible method in this question: How to simplify my statefull interlaced modal dialogs in ASP.NET MVC

这个问题有一个非常简单、灵活和可扩展的方法:How to简化我在 ASP.NET MVC 中的 statefull interlaced modal dialogs