在MVC 5中动态创建按钮 [英] Creating buttons dynamically in MVC 5

查看:61
本文介绍了在MVC 5中动态创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数据库读取数据,并在页面加载时动态创建一个与视图上的行数一样多的按钮.

I need to read data from database and create buttons as much as rowcount on a view dynamically on the pageload.

同样,按钮文本也将从数据库中获取.

Also the button text is going to be taken from database too.

即;数据表中的3行,3个按钮及其文本来自同一数据表中的字段.

i.e; 3 rows in datatable, 3 buttons with their texts from fields in same datatable.

我无法找到一个在页面加载时创建按钮的示例.我遇到的大多数动态按钮"示例都是通过单击按钮触发的.

I couldn't manage to find an example to create buttons on page load. Most of the "dynamic buttons" examples I came across are triggered by a button click.

我想我要在控制器的[HttpGet]中读取数据.在那里我可以获得必要的行数和数据,但是我不知道从那一点创建按钮.

I suppose I'm going to read the data in a [HttpGet] in the controller.There I can get the necessary row count and data, but I have no idea about creating the buttons from that point.

推荐答案

您应该可以使用剃须刀来实现所需的功能.

You should be able to achieve what you want by using razor.

请参见以下示例:剃刀C#循环

只需在模型中提供一个列表,就可以对其进行迭代.

Just provide a list in the model, and you can iterate over this.

为澄清起见进行

您可以执行以下操作:

<ul>
    @foreach (var x in Model.YourList)
    {
        <li>
            <button type="button">@x.ButtonName</button>
        </li>
    }
</ul>

然后可以根据需要在控制器中填充列表.返回视图后,将在您寻求时呈现按钮列表.

You can then populate the list however you want in your controller. Once it is returned with the view, the list of buttons will be rendered as you seek.

这篇关于在MVC 5中动态创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆