ASP.NET MVCでセカンダリアクション(フィールドの計算)を実行するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/649513

  •  19-08-2019
  •  | 
  •  

質問

フォームの送信とは異なるアクションであるASP.NET MVCビューでいくつかの計算を行う必要があります。 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);
}

これを呼び出して、返されるビューに同じモデルデータ(計算された変更を含む)を持たせるには、Html.ActionLink構文は何を必要としますか?あるいは、これを達成する別の方法はありますか?

Ajax.ActionLinkメソッドも試しましたが、同じ問題に遭遇しました

編集:<!> quot;送信ボタンに名前を付け、コントローラーメソッドで送信された値を調べます<!> quot; ここに示されているメソッド私が探していたもの。

役に立ちましたか?

解決

[コメントを見た。この回答をここに再投稿して、質問に解決済みのマークを付け、コミュニティwikiにマークを付けて、返信を得られないようにします-Dylan]

送信ボタンに名前を付け、コントローラーメソッドで送信された値を調べます。

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

}

他のヒント

アクションリンクは、アクションにリンクするだけです。 <a href="action">action</a>タグに変換されます。リンク先のアクションは、直前のページの状態を認識しません。

おそらくアクションに「POST」する必要がありますが、オブジェクトではなくフォームデータのみを送信します(mvcはフォームフィールドをオブジェクトに自動的にマッピングできます)。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top