私のselectedActiveRolesとselectedInactiveRolesは、Ajax経由でサブミットするときに私のビューモデルの一部としてコントローラに送られず、特にこれに対処するものが見つかりませんでした。私がちょうどフォームを投稿していたとき、それはうまくいきました。ASP.NET MVC AJAXフォームSubmit/PostアクションでListBoxFor/MultiSelectListを消失して送信する
マイ形式:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "editForm", @class = "form-horizontal" }))
{
<div class="row">
<div id="dualListBoxContainer">
<div class="form-group">
<label class="col-xs-2 control-label">Available Roles:</label>
<div class="col-xs-8 input-group">
@Html.ListBoxFor(model => model.SelectedInactiveRoles, new MultiSelectList(Model.InactiveRoles, "RoleId", "Description", @Model.SelectedInactiveRoles), new { @class = "form-control", name = "inactiveRoles", multiple = "multiple", size = 5 })
</div>
</div>
<div class="form-group">
<div class="col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-2 text-center">
<button type="button" class="btn btn-sm btn-primary" id="addRole"> <i class="fa fa-arrow-down"></i> Add </button>
<button type="button" class="btn btn-sm btn-primary" id="removeRole"> Remove <i class="fa fa-arrow-up"></i> </button>
</div>
</div>
<div class="form-group">
<label class="col-xs-2 control-label">Active Roles:</label>
<div class="col-xs-8 input-group">
@Html.ListBoxFor(model => model.SelectedActiveRoles, new MultiSelectList(Model.ActiveRoles, "RoleId", "Description", @Model.SelectedActiveRoles), new { @class = "form-control", name = "activeRoles", multiple = "multiple", size = 5 })
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-2 text-right">
<button type="submit" class="btn btn-primary" id="saveChanges">Save Changes <i class="fa fa-save"></i></button>
</div>
</div>
</div>
</div>
}
モデル:
public class MyViewModel
{
public User User { get; set; }
public Client Client { get; set; }
public IEnumerable<Role> ActiveRoles { get; set; }
public IEnumerable<Role> InactiveRoles { get; set; }
public IList<int> SelectedActiveRoles { get; set; }
public IList<int> SelectedInactiveRoles { get; set; }
}
JS/jQueryの:
$('#editForm')
.submit(function(e) {
e.preventDefault();
// modifies the 'selected' options on the list
// before finally being submitted by the browser
$('#SelectedInactiveRoles, #SelectedActiveRoles')
.prop('selected', true);
$.ajax({
url: $("#editForm").attr('action'),
type: 'post',
data: $(this).serialize(),
success: function() {
$("#modalContainer")
.fadeOut(500,
function() {
$("#modalContainer").empty();
});
}
});
});
コントローラー:
public ActionResult Edit([Bind] MyViewModel)
{
return null;
}
徹底的な対応に感謝します。私はSelectList要素がMultiSelectListから派生していると信じています。この文脈では、いずれかうまくいくようです。 変更を組み込んだ後で、最終的に問題が発生したと考えられます: '$( 'SelectedInactiveRoles、#SelectedActiveRoles')prop( 'selected'、true); ' select要素内のオプションを選択する必要がありました: '$( '#SelectedInactiveRoles option、#SelectedActiveRoles option')prop( 'selected'、true);' MultiSelectListまたはSelectListで複数の値を渡すには、何かを選択する必要があります。ヌル。 – Switch386
あなたの答えを徹底的に選択し、 'var formData = $(this).serialize();'を追加する必要があったためです。ありがとう! – Switch386
オプションをあるリストボックスから別のリストボックスに移動しようとしていた他のjavascriptもありますか? (そうであれば、これを再検討し、チェックリストボックスのUIに行くべきだ) –