简体   繁体   English

如何将 htmlhelper 的值从表(视图)传递给控制器​​?

[英]How to pass value of htmlhelper from table (view) to controller?

I want to update only a single value from a row on click of the update button against a specific ID, the id is available at controller but the updated value field is not available so when I press the update button controller updated the database but store a null value in DB.我只想根据特定 ID 单击更新按钮更新一行中的单个值,该 ID 在控制器上可用,但更新后的值字段不可用,因此当我按下更新按钮时,控制器更新了数据库但存储了一个DB 中的空值。

//View Code
<tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(m => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(m => item.TagName)
                </td>
                <td>
                    @Html.DisplayFor(m => item.TagCategory)
                </td>
                    <td>
                     @Html.TextBoxFor(m => item.TagValue, new { htmlAttributes = new { @class = "form-control" } })
                    </td>
                <td>
                    @Html.ActionLink("Update", "Update", new { id = item.Id}, new { htmlAttributes = new { @class = "form-control" } })
                </td>
            </tr>
        }
    </tbody>
//Controller Code
 public ActionResult Update([Bind(Include = "Id,TagValue") ]Tag tags)
    {
        var data = db.Tags.Where(x => x.Id == tags.Id).FirstOrDefault();
        if (data!=null)
        {
            data.TagValue = tags.TagValue;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View();
    }

So right now your update button makes a GET request.所以现在你的更新按钮发出一个 GET 请求。 The database is storing null because when you create the ActionLink you are not passing in the TagValue as a parameter because with GET requests the variables need to be passed in the url.数据库存储空值,因为当您创建 ActionLink 时,您没有将 TagValue 作为参数传递,因为 GET 请求需要在 url 中传递变量。 You could add the TagValue as a parameter like this您可以将 TagValue 添加为这样的参数

@Html.ActionLink("Update", "Update", new { id = item.Id, TagValue = item.TagValue }, new { htmlAttributes = new { @class = "form-control" } })

I don't think this will work the way you want though because I am assuming you want the user to be able to change the value of TagValue and then hit update to save the value.我不认为这会以您想要的方式工作,因为我假设您希望用户能够更改 TagValue 的值,然后点击更新以保存该值。 What this solution does is it just grabs the original value of whatever TagValue is.这个解决方案所做的只是获取 TagValue 的原始值。

If you want to be able to edit the TagValue and then submit you could change your html to look like this如果您希望能够编辑 TagValue 然后提交,您可以将 html 更改为如下所示

<tbody>
@foreach (var item in Model)
{
    <tr>
        <form action="@Url.Action("Update")" method="post">
            <td>
                @Html.HiddenFor(m => item.Id, new { @Name = "Id" })
                @Html.DisplayFor(m => item.Id)
            </td>
            <td>
                @Html.DisplayFor(m => item.TagName)
            </td>
            <td>
                @Html.DisplayFor(m => item.TagCategory)
            </td>
            <td>
                @Html.TextBoxFor(m => item.TagValue, new { @class = "form-control", @Name="TagValue"  })
            </td>
            <td>
                <input type="submit" value="Update" />
            </td>
        </form>
    </tr>
}

This will create a form for every Tag you have and then generate a POST request when you hit submit.这将为您拥有的每个标签创建一个表单,然后在您点击提交时生成一个 POST 请求。

vikscool's comment is also another good solution you could use. vikscool 的评论也是您可以使用的另一个很好的解决方案。

您只需要将您的视图与您发送和接收数据的表绑定,您可以使用Html Begin Forms 来完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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