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

pau*_*hit 2 asp.net-mvc view actionresult

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

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);
}
Run Code Online (Sandbox Code Playgroud)

我的Html.ActionLink语法需要什么才能让我调用它并使返回的视图具有相同的模型数据(具有计算的更改)?或者,还有另一种方法可以实现这一目标吗?

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

编辑:"为您的提交按钮命名,然后检查控制器方法中提交的值" 这里显示的方法是我正在寻找的.

Dyl*_*tie 6

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

为提交按钮指定名称,然后在控制器方法中检查提交的值:

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

张贴到

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"));
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 您如何将数据传递给相应的操作? (3认同)