【问题标题】:Why isn't my form posting back my td information?为什么我的表格没有发回我的 td 信息?
【发布时间】:2020-01-11 13:59:30
【问题描述】:

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

当我点击这个按钮时它应该返回值

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

但由于某种原因,它没有抓取 &lt;td&gt;@Html.DisplayTextFor(x =&gt; x.ServerID)&lt;/td&gt; 之类的内容并将其发回

这是剃须刀

@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.net asp.net-mvc razor


    【解决方案1】:

    试试

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

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

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

    【讨论】:

    • 如果我在var model = serverModel; 上设置断点,则为空。这就是问题所在,发布表单时未设置属性。是否重定向都没关系,我只是尝试先检索值。
    • 啊,我现在明白了。见编辑。 DisplayTextFor 不会将属性呈现为表单字段 - 它只会呈现文本。
    • 有趣!这似乎是一个“hacky”解决方案,这样做更合适的方法是什么?是否有任何其他 razor 语法可用于显示将作为表单数据发送的一段文本?
    • 你没有编辑数据,所以它是有效的......如果它是一个文本框 - TextBoxFor() 只会呈现一个字段 - 一个 &lt;input type=text&gt; 控件将包含在有效负载中在帖子上...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2014-04-12
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多