如何最好地实现保存|保存并关闭|取消在ASP.NET MVC 3 RC表单操作 [英] How to best implement Save | Save and Close | Cancel form actions in ASP.NET MVC 3 RC

查看:113
本文介绍了如何最好地实现保存|保存并关闭|取消在ASP.NET MVC 3 RC表单操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你怎么可能去提交在asp.net MVC 3 RC表单时,实现多形式的行动。

I'm wondering how you might go about implementing multiple form actions when submitting a form in asp.net mvc 3 RC.

如果我编辑的用户,比如我想有以下按钮的操作栏;

If I'm editing a user, for example I would like to have an action bar with the following buttons;

保存| 保存并关闭| 取消

保存 - 提交表单并保存,并返回到编辑画面。可以作为一个标准的输入可以轻松实现/提交按钮。这里没有什么特别。

Save - Submits the form and saves, returning you to the edit screen. Could be easily implemented as a standard input/submit button. Nothing special here.

控制器code这个看起来像

Controller code for this might look like

public ActionResult Edit(UserViewModel model)
{
  ...
  return RedirectToAction("Edit", model.Id");
}

取消 - 刚刚返回到previous屏幕。我想使用一个锚标记这一点。

Cancel - Just returns you to previous screen. I was thinking about using an anchor tag for this.

<a href="@Request.UrlReferrer" class="button">Cancel</a>

但我难倒就如何落实保存并关闭当你需要提交相同的表格数据。我想知道,也许有一个为空的密切参数?

But I'm stumped on how to implement "Save and Close" when you need to submit the same form data. I was wondering about having a nullable close param maybe?

public ActionResult Edit(UserViewModel model, bool? close)
{
  ...
  return  close.GetValueOrDefault(false) ? RedirectToAction("Index", model.Id" : RedirectToAction("Edit", model.Id");
}

但我怎么与表单一起在这种情况下提出这个额外的参数?

But how do I submit this extra param along with the form in this case?

如果可能的话,我想有处理提交在上述样机单一形式的行动。

If possible, I'd like to have a single form action for handling the submit as in the above mockup.

我也有兴趣,如果其他人想出了解决这个主意良好的用户交互模型。

I'm also interested if anyone else has come up with a nice user interaction model around this idea.

解决方案

最后我用下面,而是奥马尔的建议传递一个字符串,我参加了一个枚举,所以我没有做字符串检查我的所有控制器。

I ended up using Omar's suggestion below but instead of passing in a string I took in an enum so I don't have to do string checks in all my controllers.

public ActionResult Edit(UserViewModel model, FormAction actionType)
{
  // pre-check
  if (actionType == FormAction.Cancel)
     // just return user to previous view and don't save.

  // Save code

  if (actionType == FormAction.Save)
     return ...
  else if (actionType == FormAction.SaveAndClose)
     ....
}

由于我想要一个友好的保存并关闭文本的&LT;输入&GT; 按钮,但希望使用一个枚举我实现了一个自定义的模型绑定器为FormAction该做的解析。

Because I wanted a friendlier "Save and Close" text on the <input> button but wanted to use an enum I implemented a custom ModelBinder for FormAction that did the parsing.

我没有使用&LT;按钮&GT; 标记,因为主题化已经到位&LT;输入&GT; 标记。

I didn't use a <button> tag because the theming was already in place for <input> tags.

推荐答案

您可以用相同的名称属性,但不同的<$ C $形式多个提交按钮C>值属性。点击它曾经按钮,相关的将被发送到服务器。

You can have multiple submit buttons in a form with the same name attribute but different value attributes. Which ever button is clicked, the associated value will be posted to the server.

您可以使用一个简单的链接取消,但无论如何,我会把它作为一个按钮。

You can use a simple link for Cancel but I'll include it as a button anyway.

<input type="submit" name="actionType" value="Save" />
<input type="submit" name="actionType" value="Save and Close" />
<input type="submit" name="actionType" value="Cancel" />

而在你的行动,测试值。

And in your action, test for the values.

public ActionResult Edit(string actionType)
{
    if(actionType == "Save")
    {
        // Save action
    }
    else if (actionType == "Save and Close")
    {
        // Save and quit action
    }
    else
    {
        // Cancel action
    }
}

如果你不喜欢在属性长文本,你可以使用标准的HTML &LT;按钮&GT; 标签,它可以让你定义一个单独的值和独立的文本。

If you don't like having the long text in the value attribute, you can use standard HTML <button> tag which lets you define a separate value and separate text.

这篇关于如何最好地实现保存|保存并关闭|取消在ASP.NET MVC 3 RC表单操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆