如何在asp.net mvc中将隐藏字段值从一个视图传递到另一个视图

时间:2022-12-02 10:13:39

I have parsed the json string and displayed it in the form of table and against each record I placed a "Edit" button. My code is :

我已经解析了json字符串并以表格的形式显示它,并且针对每条记录我放置了一个“编辑”按钮。我的代码是:

for (var i = 0 ; i < data.Homes.length ; i++) {
results += "<form><tr><td>" + data.Homes[i].ID + "</td><td>" + data.Homes[i].Name + "</td>";
 results += "<td><a href=\"#\" onclick=\"\">Edit</a></td>";
 results += "<input type=\"hidden\" value=\"" + data.Homes[i].ID + "\" />";
 results += "</tr></form>";
     }  

I want the "Edit" Button to link to another view , and I am passing the id in a hidden field. Through id I can query database and get the paritcular record against this id. But I don't know how to access the value of the hidden field in the view.

我希望“编辑”按钮链接到另一个视图,我将id传递给隐藏字段。通过id,我可以查询数据库并获取针对此id的paritcular记录。但我不知道如何访问视图中隐藏字段的值。

I need help in this. Thanks in advance

我需要帮助。提前致谢

2 个解决方案

#1


1  

Give the hidden field a name attribute, and then add an input variable to the action method you're submitting to:

为隐藏字段提供名称属性,然后将输入变量添加到您要提交的操作方法中:

View:

视图:

<input type='hidden' name='myIdField' />

Controller Action:

控制器动作:

[Post]
public ActionResult SomePostMethod(int myIdField)
{
....
}

#2


0  

As your edit is link , it is not going to do the post to the controller . One of the simple way to get it through the querystring make your edit URL like this

由于您的编辑是链接,因此不会向控制器发送帖子。通过查询字符串获取它的简单方法之一就是使您的编辑网址如下所示

                 Edit?Id=data.Homes[i].ID 

Now you can access the querystring anywhere either in client side or server side.

现在,您可以在客户端或服务器端的任何位置访问查询字符串。

#1


1  

Give the hidden field a name attribute, and then add an input variable to the action method you're submitting to:

为隐藏字段提供名称属性,然后将输入变量添加到您要提交的操作方法中:

View:

视图:

<input type='hidden' name='myIdField' />

Controller Action:

控制器动作:

[Post]
public ActionResult SomePostMethod(int myIdField)
{
....
}

#2


0  

As your edit is link , it is not going to do the post to the controller . One of the simple way to get it through the querystring make your edit URL like this

由于您的编辑是链接,因此不会向控制器发送帖子。通过查询字符串获取它的简单方法之一就是使您的编辑网址如下所示

                 Edit?Id=data.Homes[i].ID 

Now you can access the querystring anywhere either in client side or server side.

现在,您可以在客户端或服务器端的任何位置访问查询字符串。