? ASP.NET MVCにはテーブルやチェアの知識はありません。これは、モデル、コントローラ、およびビューで動作します。
<select>
ボックスをレンダリングしたビューがあり、ユーザーがこの選択ボックスの値を変更して、コントローラアクションを呼び出すときに、新しく選択した値をサーバーに渡して、処理。私は正しい?
このドロップダウンの変更イベントを購読するには、ビューでjavascriptを使用する必要があります。次に、あなたはいくつかの可能性を持っています:
- 選択した値をサーバーに送信するために、AJAX要求を使用します。
- 現在のブラウザを(
window.location.href
を使用して)コントローラのアクションにリダイレクトし、選択した値をアクション引数として渡します。
要件に応じて、最適な方法を選択することができます。
私はjQueryのを使用しての例で最初のアプローチを説明してみましょう:
<script type="text/javascript">
$(function() {
// subscribe to the .change event of the dropdowns
$('select').change(function() {
// fetch the newly selected value
var selectedValue = $(this).val();
// send it as an AJAX request to some controller action
$.post('@Url.Action("Foo")', { value: selectedValue }, function(result) {
// this will be invoked in the case of success of the AJAX call
// TODO: do something with the results returned by the controller action
});
});
});
</script>
とAJAX要求を処理するあなたのコントローラのアクション:
[HttpPost]
public ActionResult Foo(string value)
{
// this action will be invoked when the user changes the selection
// in one of the dropdown lists and passed the newly selected value.
// So using this new value you could perform the desired processing
// on your model here and return a view. Since this action was invoked
// with an AJAX call you could return a JSON string to the client indicating
// the success or failure of whatever processing you were intending to perform
// here
...
}
UPDATE:
選択肢が変更されたときにドロップダウンを含むフォームを送信したい場合は、次のものを使用してください:
<script type="text/javascript">
$(function() {
// subscribe to the .change event of the dropdowns
$('select').change(function() {
// trigger the submission of the containing form:
$(this).closest('form').submit();
});
});
</script>
テーブルはかなり完全な形式です。すべてのデータがドロップダウンを含むテーブルに存在します。選択に基づいてモデルのどのメンバーが表示されるかを更新したいと思います。たとえば、私のモデルが車で、リストに赤い車の情報だけがレンダリングされるはずです。私はすべてのデータがすでにフィルタリングの問題であるビューにアクセス可能であるため、コントローラに行かなければならないとは思わない。 –
@atbyrd、フォームを提出したいですか?このケースでは、私が私の答えに示したように、ドロップダウンの変更イベントを購読して、それを含むフォームの提出を手動でトリガーすることができます: '$(this).closest( 'form')。 '。それを含めて私の答えを更新します。 –
私はこの$( "select")を使って試しました。(function(){ alert( "hello"); });変更イベントを購読するには何も起こりません。 –