Return Json 数据来自 Core/knockout 中的 POST 操作
Return Json data from POST action in Core/knockout
我正在使用 .NET Core 中的 Knockout JavaScript 库。我有以下 POST 操作成功接收和处理来自我的视图的 Json 数据。我无法开始工作的是 return 给客户:
1) 理想情况下我希望它 return 更新回视图。但是,这个 .NET Core 似乎有问题,这意味着映射不起作用。 None 以下作品:
return Json(JsonConvert.SerializeObject(salesOrderViewModel));
return Json(salesOrderViewModel);
return Json( new { salesOrderViewModel });
2) 但其他 return 似乎也没有任何效果。例如:
//return NotFound();
//return RedirectToAction("Index");
//return View("Index");
//return DoSomething();
}
private IActionResult DoSomething()
{
return RedirectToAction("Index", "Bird");
}
下面我粘贴了视图、客户端视图模型和完整的控制器操作。谁能告诉我为什么我不能 return 从我的控制器操作中得到任何东西?
查看:
@model Birder2.ViewModels.SalesOrderViewModel
@using Newtonsoft.Json
@{
ViewBag.Title = "Sales Order Details";
}
@{
string data = JsonConvert.SerializeObject(Model);
}
@section scripts{
<script src="~/js/knockout-3.4.2.js"></script>
<script src="~/js/knockout.mapping-latest.js"></script>
<script src="~/js/salesorderviewmodel.js"></script>
<script type="text/javascript">
var salesOrderViewModel = new SalesOrderViewModel(@Html.Raw(data));
ko.applyBindings(salesOrderViewModel);
</script>
}
<p data-bind="text: MessageToClient"></p>
<div>
<div>
<label>Customer Name:</label>
<input data-bind="value: CustomerName" />
</div>
<div>
<label>P.O. Number:</label>
<input data-bind="value: PONumber" />
</div>
</div>
<p><button data-bind="click: save">Save</button></p>
客户端视图模型:
SalesOrderViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, {}, self);
self.save = function () {
$.ajax({
url: "/SalesOrders/Save/",
type: "POST",
data: ko.toJSON(self),
headers:
{
"content-type": "application/json; charset=utf-8"
},
success: function (data) {
if (data.salesOrderViewModel)
ko.mapping.fromJS(data.salesOrderViewModel, {}, self);
},
});
};
};
完整的控制器动作在这里:
//[HttpPost]
//public JsonResult Save([FromBody]SalesOrderViewModel salesOrderViewModel)
[HttpPost]
public IActionResult Save([FromBody]SalesOrderViewModel salesOrderViewModel)
{
SalesOrder salesOrder = new SalesOrder();
salesOrder.CustomerName = salesOrderViewModel.CustomerName;
salesOrder.PONumber = salesOrderViewModel.PONumber;
_context.SalesOrders.Add(salesOrder);
_context.SaveChanges();
salesOrderViewModel.MessageToClient = string.Format("{0}’s sales order has been added to the database.", salesOrder.CustomerName);
return Json(JsonConvert.SerializeObject(salesOrderViewModel));
//return Json(salesOrderViewModel);
//return Json( new { salesOrderViewModel });
//return NotFound();
//return RedirectToAction("Index");
//return View("Index");
//return DoSomething();
}
数据绑定成功,数据库更新)。没有任何内容 return 发送给客户端。
对 View 进行了更改,以便我们正确绑定 knockout 并在 success 方法中使用正确的对象。
我正在使用 .NET Core 中的 Knockout JavaScript 库。我有以下 POST 操作成功接收和处理来自我的视图的 Json 数据。我无法开始工作的是 return 给客户:
1) 理想情况下我希望它 return 更新回视图。但是,这个 .NET Core 似乎有问题,这意味着映射不起作用。 None 以下作品:
return Json(JsonConvert.SerializeObject(salesOrderViewModel));
return Json(salesOrderViewModel);
return Json( new { salesOrderViewModel });
2) 但其他 return 似乎也没有任何效果。例如:
//return NotFound();
//return RedirectToAction("Index");
//return View("Index");
//return DoSomething();
}
private IActionResult DoSomething()
{
return RedirectToAction("Index", "Bird");
}
下面我粘贴了视图、客户端视图模型和完整的控制器操作。谁能告诉我为什么我不能 return 从我的控制器操作中得到任何东西?
查看:
@model Birder2.ViewModels.SalesOrderViewModel
@using Newtonsoft.Json
@{
ViewBag.Title = "Sales Order Details";
}
@{
string data = JsonConvert.SerializeObject(Model);
}
@section scripts{
<script src="~/js/knockout-3.4.2.js"></script>
<script src="~/js/knockout.mapping-latest.js"></script>
<script src="~/js/salesorderviewmodel.js"></script>
<script type="text/javascript">
var salesOrderViewModel = new SalesOrderViewModel(@Html.Raw(data));
ko.applyBindings(salesOrderViewModel);
</script>
}
<p data-bind="text: MessageToClient"></p>
<div>
<div>
<label>Customer Name:</label>
<input data-bind="value: CustomerName" />
</div>
<div>
<label>P.O. Number:</label>
<input data-bind="value: PONumber" />
</div>
</div>
<p><button data-bind="click: save">Save</button></p>
客户端视图模型:
SalesOrderViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, {}, self);
self.save = function () {
$.ajax({
url: "/SalesOrders/Save/",
type: "POST",
data: ko.toJSON(self),
headers:
{
"content-type": "application/json; charset=utf-8"
},
success: function (data) {
if (data.salesOrderViewModel)
ko.mapping.fromJS(data.salesOrderViewModel, {}, self);
},
});
};
};
完整的控制器动作在这里:
//[HttpPost]
//public JsonResult Save([FromBody]SalesOrderViewModel salesOrderViewModel)
[HttpPost]
public IActionResult Save([FromBody]SalesOrderViewModel salesOrderViewModel)
{
SalesOrder salesOrder = new SalesOrder();
salesOrder.CustomerName = salesOrderViewModel.CustomerName;
salesOrder.PONumber = salesOrderViewModel.PONumber;
_context.SalesOrders.Add(salesOrder);
_context.SaveChanges();
salesOrderViewModel.MessageToClient = string.Format("{0}’s sales order has been added to the database.", salesOrder.CustomerName);
return Json(JsonConvert.SerializeObject(salesOrderViewModel));
//return Json(salesOrderViewModel);
//return Json( new { salesOrderViewModel });
//return NotFound();
//return RedirectToAction("Index");
//return View("Index");
//return DoSomething();
}
数据绑定成功,数据库更新)。没有任何内容 return 发送给客户端。
对 View 进行了更改,以便我们正确绑定 knockout 并在 success 方法中使用正确的对象。