MVCを提出ボタンが押されてい
-
19-09-2019 - |
質問
私は二つのボタンで自分のMVCフォーム:
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
からコントローラのアクションしたいかって押されているか?
解決
両方の送信ボタンに同じ名前を付けます
<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />
次に、コントローラーで送信の値を取得します。クリックしたボタンのみがその値を渡します。
public ActionResult Index(string submit)
{
Response.Write(submit);
return View();
}
もちろん、その値を評価して、スイッチブロックで異なる操作を実行できます。
public ActionResult Index(string submit)
{
switch (submit)
{
case "Save":
// Do something
break;
case "Process":
// Do something
break;
default:
throw new Exception();
break;
}
return View();
}
他のヒント
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
コントローラーアクションで:
public ActionResult SomeAction(string submit)
{
if (!string.IsNullOrEmpty(submit))
{
// Save was pressed
}
else
{
// Process was pressed
}
}
これはより良い答えですので、ボタンのテキストと値の両方を持つことができます。
http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-same-form.aspx
</p>
<button name="button" value="register">Register</button>
<button name="button" value="cancel">Cancel</button>
</p>
とコントローラー:
public ActionResult Register(string button, string userName, string email, string password, string confirmPassword)
{
if (button == "cancel")
return RedirectToAction("Index", "Home");
...
要するに、その送信ボタンですが、名前属性を使用して名前を選択します。これは、コントローラーメソッドパラメーターの名前またはボタンに義務付けられていないため、好きなように呼び出すことができます。
以下のような名前のタグからボタンを識別できます。あなたはあなたのコントローラーでこのように確認する必要があります
if (Request.Form["submit"] != null)
{
//Write your code here
}
else if (Request.Form["process"] != null)
{
//Write your code here
}
カスタムマルチトンアトリブを使用して、本当に簡単に手順に従うことができる非常に素晴らしくシンプルな方法を紹介します。
要約するには、次のようなボタンを送信します。
<input type="submit" value="Cancel" name="action" />
<input type="submit" value="Create" name="action" />
このようなあなたの行動:
[HttpPost]
[MultiButton(MatchFormKey="action", MatchFormValue="Cancel")]
public ActionResult Cancel()
{
return Content("Cancel clicked");
}
[HttpPost]
[MultiButton(MatchFormKey = "action", MatchFormValue = "Create")]
public ActionResult Create(Person person)
{
return Content("Create clicked");
}
そして、このクラスを作成します:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultiButtonAttribute : ActionNameSelectorAttribute
{
public string MatchFormKey { get; set; }
public string MatchFormValue { get; set; }
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
return controllerContext.HttpContext.Request[MatchFormKey] != null &&
controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue;
}
}
// Buttons
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
// Controller
[HttpPost]
public ActionResult index(FormCollection collection)
{
string submitType = "unknown";
if(collection["submit"] != null)
{
submitType = "submit";
}
else if (collection["process"] != null)
{
submitType = "process";
}
} // End of the index method
簡単にするために、ボタンを次のように変更できると思います。
<input name="btnSubmit" type="submit" value="Save" />
<input name="btnProcess" type="submit" value="Process" />
あなたのコントローラー:
public ActionResult Create(string btnSubmit, string btnProcess)
{
if(btnSubmit != null)
// do something for the Button btnSubmit
else
// do something for the Button btnProcess
}
ペンダントヘッドの販売ならない答えCoppermillがって答えはずっと前から気付いていた。私のポストがある人を求めるソリューション。最初にしています"ということをWDuffyソリューションは完全に正しい"で、私のソリューション(ていない鉱山を録音させていただく場合がありまその他の要素でのプレゼンテーション層圏から独立したコントローラでコントローラに依存し"価値"に使用される表示のラベルにボタンを押すと、この機能に重要なその他の言語を学んでいます。).
この溶液に、異なる名前:
<input type="submit" name="buttonSave" value="Save"/>
<input type="submit" name="buttonProcess" value="Process"/>
<input type="submit" name="buttonCancel" value="Cancel"/>
とを指定しなければなりませんの名称はボタンの引数としての行動は以下のように:
public ActionResult Register(string buttonSave, string buttonProcess, string buttonCancel)
{
if (buttonSave!= null)
{
//save is pressed
}
if (buttonProcess!= null)
{
//Process is pressed
}
if (buttonCancel!= null)
{
//Cancel is pressed
}
}
ユーザー提出のページにボタンの引数はしてい値とする。思いこがある。
更新
この答えはかなり古いという考え直思います。も上記溶液を良い状況に渡すパラメータモデルのプロパティを表示します。かわいは自分で、ベストソリューションプロジェクトに関する
両方のボタンに名前を付け、フォームから値を確認します。
<div>
<input name="submitButton" type="submit" value="Register" />
</div>
<div>
<input name="cancelButton" type="submit" value="Cancel" />
</div>
コントローラー側:
public ActionResult Save(FormCollection form)
{
if (this.httpContext.Request.Form["cancelButton"] !=null)
{
// return to the action;
}
else if(this.httpContext.Request.Form["submitButton"] !=null)
{
// save the oprtation and retrun to the action;
}
}
Core 2.2 Razorページでは、この構文が機能します。
<button type="submit" name="Submit">Save</button>
<button type="submit" name="Cancel">Cancel</button>
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
return Page();
var sub = Request.Form["Submit"];
var can = Request.Form["Cancel"];
if (sub.Count > 0)
{
.......
request.form collectionを使用して見つけることができませんか?プロセスがクリックされた場合、request.form ["process"]が空になりません