我有這樣的方法:爲什麼綁定這個列表不適用於POST?
[HttpPost]
public ActionResult LaunchVisualiser(IList<VisualiserModel> selectedVisualisers, int implicitSelectedVisualiserId)
{
List<int> selectedSearchQueries = searchQueryRepository.GetSearchQueriesOfStreamsIds(selectedVisualisers.Where(v => v.Selected == true).Select(v => v.Stream.Id).ToList());
// rest of my code
}
,並在視圖:
@model List<SocialCrm.Models.VisualiserModel>
@using (Html.BeginForm("LaunchVisualiser", "Platform", new { implicitSelectedVisualiserId = ViewBag.CurrentVisualiser.Id }, FormMethod.Post))
{
for (int i = 0; i < Model.Count; i++)
{
if (Model[i].Id == ViewBag.CurrentVisualiser.Id)
{
continue;
}
@Html.HiddenFor(m => Model[i].Stream.Id)
<div class="editor-label">
@Html.CheckBoxFor(m => Model[i].Selected)
@Html.DisplayFor(m => Model[i].Name)
</div>
}
<br />
<br />
<input type="submit" class="btn btn-success" name="temp" value="Launch Visualiser" />
}
我只是不明白,爲什麼selectedVisualisers
做的職位時總是空。
你有什麼線索嗎?我見過這樣的例子很多,也許我失去了一些東西......
UPDATE:
生成的HTML是:
<form action="/platform/launchvisualiser?implicitSelectedVisualiserId=1" method="post">
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." name="[1].Stream.Id" type="hidden" value="9"> <div class="editor-label">
<input data-val="true" data-val-required="The Selected field is required." name="[1].Selected" type="checkbox" value="true"><input name="[1].Selected" type="hidden" value="false">
test java hiring visualiser
</div>
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." name="[2].Stream.Id" type="hidden" value="8"> <div class="editor-label">
<input data-val="true" data-val-required="The Selected field is required." name="[2].Selected" type="checkbox" value="true"><input name="[2].Selected" type="hidden" value="false">
vis test
</div>
<br>
<br>
<input type="submit" class="btn btn-success" name="temp" value="Launch Visualiser">
</form>
,這在表單數據發送到服務器(檢查Chrome瀏覽器):
[1].Stream.Id:9
[1].Selected:true
[1].Selected:false
[2].Stream.Id:8
[2].Selected:false
謝謝!正如你所說,「繼續」聲明是問題... –