厳密に型指定されたビューでは、私のビューモデルからListから "instruments"で埋められたドロップダウンリストがあります。ユーザーがアイテムをクリックするたびに、ドロップダウンリストの下にミュージシャンの楽器が表示されます。あなたは、私は楽器の満たされたリスト、ミュージシャンの楽器の空のリストやミュージシャンのオブジェクトを持って見ることができるようにjson result to strongly typed List
これは私のviewmodel
public class RegisterMusicianModel
{
public RegisterMusicianModel()
{
instrumentRepository = new InstrumentRepository();
instrumentlist = instrumentRepository.FindAllInstruments().ToList<instrument>();
musicianinstrumentlist = new List<musiciansinstrument>();
}
private InstrumentRepository instrumentRepository;
private List<instrument> instrumentlist;
private List<musiciansinstrument> musicianinstrumentlist;
public List<musiciansinstrument> Musicianinstrumentlist
{
get { return musicianinstrumentlist; }
set { musicianinstrumentlist = value; }
}
public List<instrument> Instrumentlist
{
get { return instrumentlist; }
set { instrumentlist = value; }
}
private musician music = new musician();
public musician Music
{
get { return music; }
set { music = value; }
}
}
です。 ユーザーがドロップダウンリストの選択を変更するたびに、このメソッドが呼び出され、JSONオブジェクト(楽器ではなく、ミュージシャンの楽器)が返されます。
[HttpPost]
public JsonResult AjaxInstrumentsMusician(int id)
{
musiciansinstrument mi = new musiciansinstrument();
mi.idInstruments = id;
mi.Instrumentname = instrumentrepo.GetInstrument(id).name;
return Json(mi);
}
スクリプト:
$(document).ready(function() {
$('#Instrumentlist').change(function (e) {
var myval = $('#Instrumentlist').val();
alert(myval);
$.ajax({
url: "/Account/AjaxInstrumentsMusician",
type: "POST",
data: { id: myval },
success: function (result) {
alert(result.Instrumentname);
},
error: function() {
alert('failed');
}
});
});
});
私が好きな場所私はそれは大丈夫だ、私の見解ではJSONオブジェクトのプロパティを表示することができます。しかし、私が本当に望むのは、強く型付けされたオブジェクト(ビューモデルからの空のリスト)にそれを追加することです。だから私はHTTPPOSTメソッドで私のviewmodelパラメータからアクセスすることができます。私はFormCollection、余分なパラメータ、ViewBags、の大ファンではありません...
私はコンセプトJSONにちょっと新しく、私はgoogleでこれについての良い説明を見つけることができませんでした。これは可能ですか?この問題を解決する別の優雅な方法がありますか?
あなたがしたいことはほとんど理解できません。ユーザーが楽器を選択したときにリストに入力しますか?表示する前にそれを設定しますか? jsonメソッドでモデルを使用しますか? – Rattlemouse
私は自分の見解で使うことができるJSONの結果を持っています。厳密に型指定されたビューであるため、私はviewmodelで定義されたListに追加したいと思います。ユーザーがフォームを送信するときにJSONの結果にアクセスできることを望んでいました。 '[HttpPost] public ActionResult RegisterMusician(RegisterMusicianModel model ){model.musicianinstrumentlist; nullを返す。 } 'これは実現可能ですか? – BBQ