以下是该方案:
当用户选择Details
按钮时,JavaScript函数getPersonId
会按预期抓取personId。
问题出在哪里:
选择personId
后,我将Id
发送到ajax
来电。 Id
获得通过,但遇到问题:我的成功应该是什么?和传递给控制器的personIn为空
问题列表:
- 将参数发送到控制器时,Success如何工作?
- C#参数为空。
- 调用ajax方法的最佳方法是什么?是否应该在详情按钮上调用?
醇>
HTML
<div class="col-md-12">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Details</th>
</tr>
</thead>
<tbody>
@foreach (var viewModel in Model)
{
<tr>
<td>@viewModel.Name</td>
<td>@viewModel.Age</td>
<td><button data-id="@viewModel.PersonId" type="button" data-bind="click: getPersonId" class="btn btn-success" data-toggle="modal" data-target="#details-modal"><span class="fa fa-external-link-square"></span> Details</button></td>
</tr>
}
</tbody>
</table>
</div>
在HTML底部创建PersonViewModel实例
<script>
$(function () {
window.viewModel = new PersonViewModel().init(@Html.Json(Model));
});
</script>
JavaScript
function PersonViewModel() {
var self = this;
self.init = function(data) {
ko.mapping.fromJS(data, {}, self);
ko.applyBindings(self, $("#tblPerson")[0]);
return self;
}
self.getPersonId = function(vm, ev) {
var personId = $(ev.target).data('id');
return personId;
}
self.getPersonEntity = function() {
$.ajax({
type: "POST",
url: '/Person/RetrievePersonEntity',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: ko.toJSON(self.getPersonId),
success: function() {} // not sure what I need here...
});
}
}
C#Controller
[HttpPost]
public JsonResult RetrievePersonEntity(string personId)
{
var result = _personOrchestrator.RetrievePersonEntity(personId);
return Json(result);
}
答案 0 :(得分:2)
试试这个:
self.getPersonEntity = function() {
$.ajax({
type: "POST",
url: '/Person/RetrievePersonEntity?personId=' + self.getPersonId(vm, ev),
data: ko.toJSON(self.getPersonId),
success: function() {
return self.getPersonId
}
});
}
答案 1 :(得分:0)
你可以尝试这样
self.getPersonEntity = function() {
$.ajax({
type: "POST",
url: '/Person/RetrievePersonEntity' ,
data: {personId:self.getPersonId(vm, ev)},
success: function() {
return self.getPersonId
}
});
}