最好的方法是什么(选择行,单击按钮将其删除) [英] What's the best way for (select row, delete it on button click)
问题描述
我找不到解决问题的办法. 这是我正在从事的MVC项目.
I could not find any solution for my problem. This is a MVC project I am working on.
在GridView中,我该怎么做:Click on row and then click on button to delete this selected or clicked row.
In the GridView how can I do this: Click on row and then click on button to delete this selected or clicked row.
不需要任何带有自动选择按钮的解决方案.
Don't need any solution with automatic Select button.
所以
-
鼠标单击该行
Mouse click on the row
获取其ID或任何值
重定向到我的函数+ ID的按钮.
Button that redirect to my function + Id.
gridview是不可能的吗?如果使用Table会更好吗?
Is it impossible with gridview? would it be better if I use Table?
这是我尝试过的:
//To get the id
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.RowIndex.ToString();
string id = DataBinder.Eval(e.Row.DataItem, "Id").ToString();
e.Row.Attributes.Add("rowid", id);
}
}
我的javascript按钮
My javascript an button
<a href='<%=ResolveUrl("~/Producter/Delete?id=" ) %>' ID="HyperLink1">Delete</a>
<script type ="text/javascript" language="javascript">
//every time a row is clicked this script will perform the following actions:
$("tr").click(function () {
var clicked = $(this);
//get the row id from the currently cliked row
var rowid = clicked.attr("rowid");
//get the value of href attribute from the link with id 'HyperLink1'
var link = $("#HyperLink1").attr("href");
//remove any previously appended values
var linkTokens = link.split("=");
linkTokens[1] = "";
link = linkTokens.join("=");
//append the current row id to the link
link = link + rowid;
//set the href attribute of your link to the new value
$("#HyperLink1").attr("href", link);
});
</script>
Got id =一直未定义.
Got id = undefined all the time.
推荐答案
如果您使用的是MVC,我认为您对GridView的使用都错了,正如我从后面的代码中看到的那样. 这是一个与MVC不兼容的服务器控件,因为它依赖于PostBack和ViewState. 您可以以面向MVC的方式"轻松完成此操作:
If you're using MVC, I think you got the usage of GridView all wrong as I can see from your code behind. This is a server control which isn't compatible with MVC since it depends on PostBack and ViewState. You can do it quite easily in "MVC oriented fashion":
比方说,您想从称为产品"的操作方法中显示产品"列表.
Let's say you want to show list of Products from an action method called 'Products'.
- 右键单击该控制器动作并添加新的视图.
- 弹出一个模式对话框.
- 命名为产品"的视图.
- 从列表中选择它将强烈绑定的类型,因此 产品"类.
- 选择视图类型为列表".
- 选择布局页面(如ASP.NET中的MasterPage).
- 就是这样.
- Right-Click that controller action and add new View.
- A modal dialog pops up.
- Name that view 'Products'.
- Choose from the list the type it will strongly bound to, hence 'Product' class.
- Choose the type of view as 'List'.
- Choose the layout page (Like MasterPage in ASP.NET).
- that's it.
现在,在控制器操作中,您将调用该View并将其传递给Products集合,如下所示:
Now, in the controller action, you shold call that View and pass it a collection of Products, something like that:
public ActionResult Products()
{
List<Products> products = SomeMethodToGetProducts();
return View(products);
}
简单!
您现在要做的就是在生成的视图中填充编辑",删除"等操作.
All you have to do now is populate the actions in the generated view like 'Edit', 'Delete' and so on.
这篇关于最好的方法是什么(选择行,单击按钮将其删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!