简体   繁体   中英

Why isn't my form posting back my td information?

So I'm trying to post back the value that's inside the <td> but for some reason it's not posting it back, all the properties are either null or set to their default value such as 0.

It should post back the values when I click this button

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

But for some reason it's not grabbing things like <td>@Html.DisplayTextFor(x => x.ServerID)</td> and posting it back

Here is the razor

@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>
                    }

And the Controller

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

the model properties are null or 0 as I said.

And here is the Model

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

Try

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

DisplayTextFor will only render the item as text.

Including the hidden field will include them in the payload sent to the server - as form items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM