首页 > 解决方案 > 为什么我的表格没有发回我的 td 信息?

问题描述

所以我试图发回里面的值,<td>但由于某种原因它没有发回,所有属性要么为空,要么设置为默认值,例如 0。

当我单击此按钮时,它应该回发值

<button type="submit" class="btn btn-icon waves-effect waves-light btn-info"> <i class="fa fa-wrench"></i> </button>

但由于某种原因,它没有抓取类似的东西<td>@Html.DisplayTextFor(x => x.ServerID)</td>并将其发回

这里是剃须刀

@using (Html.BeginForm("Edit", "Dashboard"))
                    {
                        <table class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Server Name</th>
                                    <th>Credentials</th>
                                    <th>Status</th>
                                    <th>Modify</th>
                                    <th>Delete</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>@Html.DisplayTextFor(x => x.ServerID)</td>
                                    <td>Data</td>
                                    <td>@Html.DisplayTextFor(x => x.Endpoint)</td>
                                    if (Model.IsRunning)
                                    {
                                        <td><span class="badge badge-success">Running</span></td>
                                    }
                                    else
                                    {
                                        <td><span class="badge badge-danger">Stopped</span></td>
                                    }
                                    <td>
                                        <button class="btn btn-icon waves-effect waves-light btn-success"> <i class="fa fa-power-off"></i> </button>
                                        <button class="btn btn-icon waves-effect waves-light btn-danger"> <i class="fa fa-stop"></i> </button>

                                        <button type="submit" class="btn btn-icon waves-effect waves-light btn-info"> <i class="fa fa-wrench"></i> </button>

                                    </td>
                                    <td>
                                        <button class="btn btn-icon waves-effect waves-light btn-danger"> <i class="fa fa-trash"></i> </button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    }

和控制器

 public ActionResult Edit(ServerModel serverModel)
        {
            var model = serverModel;
            return View();
        }

正如我所说,模型属性为 null 或 0。

这是模型

 public class ServerModel
    {
        public int ServerID { get; set; }
        public string Endpoint { get; set; }
        public bool IsRunning { get; set; }
    }

标签: c#asp.netasp.net-mvcrazor

解决方案


尝试

@Html.HiddenFor(x => x.ServerID)
@Html.DisplayTextFor(x => x.ServerID)

DisplayTextFor 只会将项目呈现为文本。

包含隐藏字段会将它们包含在发送到服务器的有效负载中 - 作为表单项。


推荐阅读