我遵循https://demos.telerik.com/kendo-ui/multiselect/addnewitem使用KendoUI Multi Select添加新项
控制器:
[HttpPost]
[Route("admin/blog/PostTag")]
public IActionResult PostTag([FromForm]TagModel models)
{
return Ok();
}
模型总是获得空值,我也尝试不使用[FromForm] PostTag(TagModel models)
或使用[FromBody],但这是行不通的。
TagModel类:
public class TagModel
{
public int Id {get;set;}
public string Tag { get; set; }
}
“查看”页面上的KendoUi:
<select asp-for="Tag" data-placeholder="Chọn tag"></select>
<script id="noDataTemplate" type="text/x-kendo-tmpl">
# var value = instance.input.val(); #
# var id = instance.element[0].id; #
<div>
No data found. Do you want to add new item - '#: value #' ?
</div>
<br />
<button class="k-button" onclick="addNew('#: id #', '#: value #')" ontouchend="addNew('#: id #', '#: value #')">Add new item</button>
</script>
<script>
function addNew(widgetId, value) {
var widget = $("#" + widgetId).getKendoMultiSelect();
var dataSource = widget.dataSource;
if (confirm("Are you sure?")) {
dataSource.add({
Id: 0,
Tag: value
});
console.log(dataSource);
dataSource.sync();
//dataSource.one("requestEnd", function (args) {
// if (args.type !== "create") {
// return;
// }
// console.log(args.type)
// var newValue = args.response[0].id;
// dataSource.one("sync", function () {
// widget.value(widget.value().concat([newValue]));
// });
//});
//dataSource.sync();
}
}
</script>
<script>
$(document).ready(function () {
var crudServiceBaseUrl = "/admin/blog";
var dataSource = new kendo.data.DataSource({
batch: true,
transport: {
read: {
url: "https://localhost:44383/admin/blog/gettags"
},
create: {
url: "https://localhost:44383/admin/blog/posttag",
type: "POST",
dataType: "jsonp" // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
},
parameterMap: function (data, type) {
if (type != "read" && data.models) {
console.log(data.models);
return { models: kendo.stringify(data.models) };
};
}
},
schema: {
model: {
id: "Id",
fields: {
Id: { type: "number" },
Tag: { type: "string" }
}
}
}
});
$("#Tag").kendoMultiSelect({
filter: "startswith",
dataTextField: "Tag",
dataValueField: "Id",
dataSource: dataSource,
noDataTemplate: $("#noDataTemplate").html()
});
});
</script>