如何解决如何使用JQuery ajax从控制器传递数据模型以进行查看
我正在尝试使用JQuery从asp.net核心应用程序中的mcv控制器获取模型。
想法是使用我的Selectionviewmodel的每个属性填充我的选择选项。
距离我开始学习C#和asp.net应用程序部署已经两个月了,如果我犯了一个大错误,对不起。
这是我的cshtml视图:
<select id="selectionContractsTypes"></select>
<select id="selectionPaymentChoices"></select>
<select id="selectionPopulationTypes"></select>
@section Scripts {
<script>
import { forEach } from "core-js/fn/dict";
$(document).ready(function () {
$.ajax({
type: "GET",url: "/AjaxSelection/GetSelectionoptions",contentType: "application/json;charset=utf-8",dataType: "json",success: function (data) {
var selCT = $("selectionContractsTypes");
$(data.ContractsTypes).each(function () {
selCT.append($("<option>").attr('value',this.val).text(this.text));
});
var selPC = $("selectionPaymentChoices");
$(data.PaymentChoices).each(function () {
selPC.append($("<option>").attr('value',this.val).text(this.text));
});
var selPT = $("selectionPopulationTypes");
$(data.PaymentChoices).each(function () {
selPT.append($("<option>").attr('value',this.val).text(this.text));
});
},error: function (response) {
alert('eror');
}
});
});
</script>
}
这是我的Selectionviewmodel类:
public class Selectionviewmodel
{
public ObservableCollection<string> ContractsTypes { get; set; }
public ObservableCollection<string> PaymentChoices { get; set; }
public ObservableCollection<string> PopulationTypes { get; set; }
}
public IActionResult Index()
{
return View();
}
[HttpGet]
public async Task<IActionResult> GetSelectionoptions()
{
var model = new Selectionviewmodel()
{
//on récup
ContractsTypes = new ObservableCollection<string>(await _businessApiService.GetTypeContratsNamesAsync()),PopulationTypes = new ObservableCollection<string>(Enum.Y(typeof(X)).ToList()),PaymentChoices = new ObservableCollection<string>(_mockedPaymentChoices),};
return Json(model);
}
我该如何解决?非常感谢:)非常感谢您的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。