首先,在div中无法识别asp-for,无法直接在view中用selected值修改model。如果想改变input的值,可以这样:
查看:
<div class="form-group row">
<label asp-for="UsColor" class="col-sm-2 col-form-label">Color</label>
<div class="col-sm-10">
<input asp-for="UsColor"/>
<div class="picker" id="picker1" />
</div>
</div>
js:
$("#picker1").colorPick({
'initialColor': '#8e44ad',
'palette': ["#1abc9c", "#16a085", "#2ecc71", "#27ae60", "#3498db", "#2980b9", "#9b59b6", "#8e44ad", "#34495e", "#2c3e50", "#f1c40f", "#f39c12", "#e67e22", "#d35400", "#e74c3c", "#c0392b", "#ecf0f1"],
'onColorSelected': function () {
console.log("The user has selected the color: " + this.color)
this.element.css({ 'backgroundColor': this.color, 'color': this.color });
$("#UsColor").val(this.color);
return this.color;
}
});
结果:
如果你想在没有表单的情况下将值传递给动作,你可以使用 ajax:
js:
$("#picker1").colorPick({
'initialColor': '#8e44ad',
'palette': ["#1abc9c", "#16a085", "#2ecc71", "#27ae60", "#3498db", "#2980b9", "#9b59b6", "#8e44ad", "#34495e", "#2c3e50", "#f1c40f", "#f39c12", "#e67e22", "#d35400", "#e74c3c", "#c0392b", "#ecf0f1"],
'onColorSelected': function () {
console.log("The user has selected the color: " + this.color)
this.element.css({ 'backgroundColor': this.color, 'color': this.color });
$.ajax({
type: "POST",
url: '@Url.Action("ChangeColor", "Test")',
data: { "color": this.color }
}).done(function (data) {
});
return this.color;
}
});
测试控制器:
public IActionResult ChangeColor(string color) {
return Ok();
}
结果: