如何使用javascript从webgrid获取id

时间:2013-09-02 07:36:24

标签: javascript asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor

我尝试制作webgrid,当我点击编辑按钮时,按钮会将值从webgrid中的第一个td传递给我的控制器,但是我无法传递值..有人可以告诉我,哪一行是错误的?

这是我的观点

 @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        firstText: "<< First",
        previousText: "< Prev",
        nextText: "Next >",
        lastText: "Last >>",
        mode: WebGridPagerModes.All,
        columns: grid.Columns(
            grid.Column(header: "Content ID", format: (item) => item.ContentID),
            grid.Column(header: "Active", format: (item) => Html.Raw("<input type=\"checkbox\" " + ((item.Active == true) ? "checked='cheked'" : "") + "disabled = 'disabled'/>")),
            grid.Column(header: "Image", format: @<img src="@Href("~/images/MobileContent/" + @item.ImageURL)" width="120px" height="50px;" />),
            grid.Column("Description"),
            grid.Column("Action", format: @<text>
                                <button class="edit-content">Edit</button>
                                <button class="remove-content">Remove</button>
                            </text>)

        )
        )
                        }
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script>
        $(".edit-content").click(function () {
            var id = $(this).find('td:first').text();
            location.href = '/MobileContent/Edit/' + id;
        });

    $('thead tr th:nth-child(1), tbody tr td:nth-child(1)').hide();
    </script>
}

这是我的控制器

public ActionResult Edit(int id)
        {
            ViewModel.MobileContent.MobileContentViewModel vm = new ViewModel.MobileContent.MobileContentViewModel();
            vm.EditContent = EditContent(id);
            return View(vm);
        }

2 个答案:

答案 0 :(得分:0)

试一试

var id = $(this).parent('tr').find('td:first').text();

答案 1 :(得分:0)

试试这个,

 <input type="button" value="Assign" onclick="myfunction(@item.ID);" />
OR
@Html.ActionLink("EditUser", "DYmanicControllerPage", "Test", new { Id = item.ID }, new { @class = "hide" })


 function myfunction(obj) {

        var id = obj;
        location.href = '@Url.Action("DYmanicControllerPage", "Test")?Id=' + id;

    }

<强>控制器

 public ActionResult DYmanicControllerPage(string Id)
        {
            var model = new RegisterModel();
            int _ID = 0;
            int.TryParse(Id, out _ID);
            if (_ID > 0)
            {
                RegisterModel register = GetRegisterUserById(_ID);
                model.ID = _ID;
                model.Name = register.Name;
                model.Address = register.Address;
                model.PhoneNo = register.PhoneNo;

            }
            return View(model);
        }