asp.net-mvc - 如何在 ASP.NET MVC 中执行辅助操作(即计算字段)?

标签 asp.net-mvc view actionresult

我需要对 ASP.NET MVC View 进行一些计算,这是与表单提交不同的操作。我尝试了通过 ActionLink 将当前模型传递到新 Controller 操作的各种方法,但模型似乎没有被传递。

public ActionResult Calculate(MuralProject proj)
{
    ProjectFormRepository db = new ProjectFormRepository();
    List<Constant> constants = db.GetConstantsByFormType(FormTypeEnum.Murals);

    proj.Materials = new MuralMaterials();
    proj.Materials.Volunteers = this.GetVolunteerCount(constants, proj);

    this.InitializeView(); 
    return View("View", proj);
}

我的 Html.ActionLink 语法需要什么才能调用它并使返回的 View 具有相同的模型数据(具有计算的更改)?或者,还有其他方法可以实现此目的吗?

我也尝试了 Ajax.ActionLink 方法,但遇到了同样的问题

编辑:“为您的提交按钮命名,然后在 Controller 方法中检查提交的值”方法显示 here这就是我一直在寻找的。

最佳答案

[看到你的评论;我将在这里重新发布这个答案,以便您可以将问题标记为已解决,并将其标记为社区维基,这样我就不会得到代表 - Dylan]

为您的提交按钮命名,然后在 Controller 方法中检查提交的值:

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="submitButton" value="Send" />
<input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>

发布到

public class MyController : Controller {
    public ActionResult MyAction(string submitButton) {
        switch(submitButton) {
            case "Send":
                // delegate sending to another controller action
                return(Send());
            case "Cancel":
                // call another action to perform the cancellation
                return(Cancel());
            default:
                // If they've submitted the form without a submitButton, 
                // just return the view again.
                return(View());
        }
    }

    private ActionResult Cancel() {
        // process the cancellation request here.
        return(View("Cancelled"));
    }

    private ActionResult Send() {
        // perform the actual send operation here.
        return(View("SendConfirmed"));
    }

}

关于asp.net-mvc - 如何在 ASP.NET MVC 中执行辅助操作(即计算字段)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/649513/

相关文章:

asp.net-mvc - 是否可以对ASP.NET-MVC中的每个 Action 应用ActionFilter

asp.net - 如何在开发过程中管理来自子域的静态内容服务

c# - 如何将 RedirectToAction 中的单个参数传递给 ASP .NET MVC 中的不同 Controller ?

c# - 如何实现用户之间的消息传递系统?

ruby-on-rails - 如何获取rails中的当前 View 路径?

c# - 在 C# MVC 中,您可以设置每个操作方法在处理其余请求之前必须执行的全局行为吗?

asp.net-mvc - 存储库模式 : One repository class for each entity?

ViewPager 的 Android Viewholder

c++ - 如何在 openGL,C++ 中改变视点

.net - 如何在 MVC 中返回 XML 字符串作为操作结果