我在删除表中的项目时遇到问题,这些值与敲除绑定并使用MVC发布请求删除项目。发生的事情是它总是删除表中的第一项..所以如果我删除第一项或最后一项,它总是会删除第一项。为什么MVC绑定错误的值?
如果我的表输出..
position preference intake Delete
pos 1 1 intake 1 <Submit button>
pos 2 1 intake 2 <Submit button>
pos 3 1 intake 3 <Submit button>
pos 3 2 intake 1 <Submit button>
pos 4 1 anything <Submit button>
点击任何提交将始终删除表格中的第一项...以使其清楚。 谢谢你的帮助。
<input type="hidden" name="position" data-bind="attr: { value: ApplyingFor }" />
<input type="hidden" name="preference" data-bind="attr: { value: Preference }" />
<!-- ko if: IsIntakes() -->
<input type="hidden" name="intake" data-bind="attr: { value: Intake }" />
<span data-bind="text: Intake"></span>
<input type="submit" formaction="@Url.Action("DeleteIntake")" formmethod="post" class="Buttons" onclick="return confirm('Are you sure you want to delete this intake?')" value="Delete">
<!-- /ko -->
<!-- ko if: !IsIntakes() -->
<input type="hidden" name="location" data-bind="attr: { value: Location }" />
<span data-bind="text: Location"></span>
<input type="submit" formaction="@Url.Action("DeleteLocation")" formmethod="post" class="Buttons" onclick="return confirm('Are you sure you want to delete this location?')" value="Delete">
<!-- /ko -->
这是完整的代码.cshtml
<div class="ColouredBlock ColouredBlock_Purple">
<h1>Intakes & Locations Tool</h1>
<div id="intakesLocations">
<fieldset>
<legend style="font-weight: bold; padding: 5px">Update</legend>
<div>
<input type="radio" name="intakesOrLocations" value="Intake" data-bind="checked: intakesOrLocations, event: {change: selectedIntakesOrLocationsChange}" /> Intakes
<input type="radio" name="intakesOrLocations" value="Location" data-bind="checked: intakesOrLocations, event: {change: selectedIntakesOrLocationsChange}" /> Locations
</div>
<div data-bind="if: intakesOrLocations">
<div style="font-weight: bold; padding: 5px; margin-top: 5px">For position:</div>
<div>
<select data-bind="
options: positions,
value: selectedPosition,
optionsCaption: '-- Select --',
event: {change: selectedPositionChange}">
</select>
</div>
</div>
</fieldset>
<br />
<div data-bind="if: selectedPosition">
<a data-bind="attr: { href: 'New.aspx?position=' + selectedPosition() + '&intake=' + isIntakes()}" class="OrangeLink TopRightHeaderLevelLink">
New <span data-bind="text: intakesOrLocations"></span>
</a>
<h2><span data-bind="text: intakesOrLocations"></span>s</h2>
<div data-bind="with: selectedPosition">
<table class="ListingTable">
<thead>
<tr>
<th data-bind="visible: false">Position</th>
<th data-bind="text: $parent.intakesOrLocations"></th>
<th>Preference</th>
<th>Order</th>
<th>Folder</th>
<th>Color</th>
<th data-bind="visible: $parent.isIntakes()">Deadline</th>
<th data-bind="visible: $parent.isIntakes()">Placeholder</th>
<th>Hidden</th>
<th>Delete</th>
</tr>
</thead>
<tbody data-bind="foreach: $parent.filter">
<tr class="Row">
<td data-bind="visible: false, text: ApplyingFor"></td>
<!-- ko if: IsIntakes() -->
<td>
<a data-bind="text: Intake, attr: { href: 'Edit.aspx?applyingfor=' + ApplyingFor() + '&intake=' + Intake() + '&preference=' + Preference()}" title="Edit" class="ArrowLink">
<span data-bind="text: Intake"></span>
</a>
</td>
<!-- /ko -->
<!-- ko if: !IsIntakes() -->
<td>
<a data-bind="text: Location, attr: { href: 'Edit.aspx?applyingfor=' + ApplyingFor() + '&location=' + Location() + '&preference=' + Preference()}" title="Edit" class="ArrowLink">
<span data-bind="text: Location"></span>
</a>
</td>
<!-- /ko -->
<td data-bind="text: Preference"></td>
<td data-bind="text: Order"></td>
<td data-bind="text: Folder"></td>
<td data-bind="text: Color, style: { color: Color }"></td>
<!-- ko if: IsIntakes() -->
<td data-bind="text: Deadline"></td>
<td>
<!-- ko if: IsPlaceholderIntake() -->
<img src="@Url.Content("~/Images/yes.gif")" />
<!-- /ko -->
<!-- ko if: !IsPlaceholderIntake() -->
<img src="@Url.Content("~/Images/no.gif")" />
<!-- /ko -->
</td>
<!-- /ko -->
<td>
<!-- ko if: Hidden() -->
<img src="@Url.Content("~/Images/yes.gif")" />
<!-- /ko -->
<!-- ko if: !Hidden() -->
<img src="@Url.Content("~/Images/no.gif")" />
<!-- /ko -->
</td>
<td>
<input type="hidden" name="position" data-bind="attr: { value: ApplyingFor }" />
<input type="hidden" name="preference" data-bind="attr: { value: Preference }" />
<!-- ko if: IsIntakes() -->
<input type="hidden" name="intake" data-bind="attr: { value: Intake }" />
<input type="submit" formaction="@Url.Action("DeleteIntake")" formmethod="post" class="Buttons" onclick="return confirm('Are you sure you want to delete this intake?')" value="Delete">
<!-- /ko -->
<!-- ko if: !IsIntakes() -->
<input type="hidden" name="location" data-bind="attr: { value: Location }" />
<input type="submit" formaction="@Url.Action("DeleteLocation")" formmethod="post" class="Buttons" onclick="return confirm('Are you sure you want to delete this location?')" value="Delete">
<!-- /ko -->
</td>
</tr>
</tbody>
</table>
</div>
<div>
<div data-bind="visible: loading" style="margin-bottom: 15px">Loading ...</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
var viewModel = new IntakesLocationsViewModel();
ko.applyBindings(viewModel, document.getElementById("intakesLocations"));
});
</script>