【问题标题】:using a button inside a bootstrap table使用引导表中的按钮
【发布时间】:2018-02-22 01:50:53
【问题描述】:

我正在尝试使用按钮从名为“Students”的控制器中调用“Edit”操作方法,以下是我的代码:

<script>
$("#table1").bootstrapTable({

    columns: [
        { field: 'fn', title: "First Name" }, { field: 'ln', title: "Last Name" }, { field: 'gender', title: "Gender" },
        { field: 'totalmarks', title: "Total Marks" }, { field: "totalcourses", title: "Total Courses Enrolled In" },
        {field: 'links', title: "Actions"}
        ],
    data: [
        @foreach (var item in Model)
        {
            int total = ((StudentsController)this.ViewContext.Controller).totc(@item.Id);
            @: { fn: '@item.FirstName', ln: '@item.LastName', gender: '@item.Gender', totalmarks: '@item.TotalMarks', totalcourses: '@total', 
            @: links: '<input type="button" value="Edit" onclick="window.location.href='@Url.Action("Edit","Students")';" />' }
        ]
});

显然它不起作用,我尝试使用 Html.ActionLink 方法并且它起作用但我需要一个按钮而不是链接。 我还是新手使用引导表,有人可以帮忙吗?

更新:忘了说 Edit 方法需要一个数字传递给它,这个数字是来自 foreach 循环的 item.Id。

【问题讨论】:

  • 为什么需要一个按钮?只需将您链接的样式设置为看起来像一个按钮。添加您的 ActionLink 如下:@Html.ActionLink("action", "controller", new { parameterName: value }, new { @class= "btn btn-primary" });
  • 这正是我所做的,我使用了一个链接:class="btn btn-default"。对我来说效果很好。

标签: javascript c# html asp.net-mvc-5


【解决方案1】:

试试这个:

<a href='@Url.Action("Edit","Students", new {id = @item.Id})' class='btn btn-default'>Edit</a>

【讨论】:

  • 好吧对不起,我忘了说 Edit 操作方法需要调用 item.id 否则它会给出一个“错误请求”作为发生的事情,但它确实尝试调用这个好的方法,现在我需要找到一种方法来从 foreach 循环中知道项目的 id。
  • 经过几次尝试后工作,似乎我不得不将结束的 '}' 单独移动到一个新行上,如果它有什么东西,它只是不会将它识别为一个结束括号。非常感谢您的帮助。
【解决方案2】:

在onClick事件中,可以试试这个吗?

'<input type="button" value="Edit" onclick="window.location.href="/Students/Edit/@item.Id""/>'

它将调用学生控制器中的编辑操作。

【讨论】:

  • 我现在试试
  • 好的,我之前尝试过的第二个并再次尝试仍然没有工作。第一个一直说“意外字符串”。
  • 首先,问题是我需要在其上附加一个数字(Id),例如:,因为 Edit 操作方法需要一个数字,它将从中获取item.Id。
  • 变成了这样:onclick="window.location.href='/Students/Edit/@item.Id'"
  • 是的,我刚刚意识到您必须附加 ID。它是否仍然抛出“Unxpected String”错误?
猜你喜欢
  • 1970-01-01
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 2018-12-27
相关资源
最近更新 更多