MAO-EYE 2017-01-08 00:04 采纳率: 100%
浏览 18

asp.net核心和ajax

I'm working on ASP.NET Core application and not sure what is right way to use ajax in it.

For example, I have model:

public class Person
    {
        public int ID { get; set; }
        public string Wiki { get; set; }
        public string Name { get; set; }
    }

Controller, for show list of items:

public async Task<IActionResult> Index()
        {
            return View(await _context.Person.ToListAsync());
        }

And view:

<table class="table">
    <thead>
        <tr>                
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>            
            <td>
                <a asp-action="Details" asp-route-id="@item.ID">@Html.DisplayFor(modelItem => item.Name)</a>
            </td>

        </tr>
}
    </tbody>
</table>

When I press on item I want to get item's details.

I have action Details(int? id) and view Details.cshtml.

And I created ViewModel:

public class PersonViewModel
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public string Birthday { get; set; }
        public string Death { get; set; }
        public string Image { get; set; }
        public string Link { get; set; }            
    }

But this information I get from Wikidata using ajax.

I can run ajax by pressing link on the item and I get json from Wikidata.

But how I can populate ViewModel? For this I need to call action Details from ajax. But ajax usually used for get information on the current page (to avoid postback). So in this case it doesn't belong here.

Or I can call ajax when loading Detals view and populate page information. But in this case I don't need ViewModel at all.

What is right way to use ajax in my application?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?