我如何进行辅助操作(即计算域)在ASP.NET MVC? [英] How do I perform a secondary action (i.e. calculate fields) in ASP.NET MVC?

查看:132
本文介绍了我如何进行辅助操作(即计算域)在ASP.NET MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个ASP.NET MVC视图,比表单提交不同的动作一些计算。我试图通过一个ActionLink的传递当前型号上到一个新的控制器操作的各种方法,但该模型似乎并没有被通过。

I need to do some calculations on an ASP.NET MVC View, an action different than the form submission. I've tried various methods of passing the current Model on to a new controller action via an ActionLink, but the model doesn't appear to be passed.

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语法需要是为了让我打电话给这并返回查看有没有相同的模型数据(与计算的变化)?另外,有另一种方式来做到这一点?

What would my Html.ActionLink syntax need to be in order for me to call this and have the returning view have the same model data (with the calculated changes)? Alternately, is there another way to accomplish this?

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

I also tried an Ajax.ActionLink method but I ran into the same problem

修改:给你的提交按钮的名称,然后在你的控制器方法检查提交的值显示<方法href=\"http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework\">here就是我一直在寻找。

Edit: "Give your submit buttons a name, and then inspect the submitted value in your controller method" method shown here is what I was looking for.

推荐答案

[锯你的意见;我将在这里重新发布这个答案让您可以标记的问题解决了,并标记社区维基所以我不明白它代表 - 迪伦]

[Saw your comments; I'll repost this answer here so you can mark the question resolved, and mark it community wiki so I don't get rep for it - Dylan]

给你的提交按钮的名称,然后检查你的控制器方法提交的值:

Give your submit buttons a name, and then inspect the submitted value in your controller method:

<% 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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