What is the best way to pass random data from a view to a controller.
从视图向控制器传递随机数据的最佳方式是什么?
I have a number of views that are identical except for a few pieces of state information. instead of creating a page for each webpage, I want to have one page and I just pass over these variables dynamically.
除了一些状态信息之外,我有一些相同的视图。我不想为每个网页创建一个页面,我只想有一个页面,然后动态地传递这些变量。
2 个解决方案
#1
1
Although not the "recommended" approach, you can make your action method accept a FormCollection as the parameter. You can then write your own logic to pull whatever data you need from that object. Basically the FormCollection will contain all fields inside the form that is posted as a key-value pair.
虽然不是“推荐”方法,但是您可以使您的操作方法接受一个FormCollection作为参数。然后,您可以编写自己的逻辑来从该对象中提取所需的任何数据。基本上,FormCollection将包含表单中作为键-值对发布的所有字段。
The signature would look like this:
签名如下:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
// logic here to
}
#2
1
Not shure about the "random data" - but maybe the question is realy about "views that are identical"
并不是对“随机数据”羞于启齿——但也许问题的关键在于“相同的视图”
Put the common parts into a partial and the differing parts into the page, if your layout allows it.
如果布局允许的话,把公共部分放到部分中,把不同的部分放到页面中。
You would have a couple of pages then, but no duplicate code.
您将有几个页面,但没有重复的代码。
Or is your problem more on the controller/model side?
还是你的问题更多地在控制器/模型方面?
#1
1
Although not the "recommended" approach, you can make your action method accept a FormCollection as the parameter. You can then write your own logic to pull whatever data you need from that object. Basically the FormCollection will contain all fields inside the form that is posted as a key-value pair.
虽然不是“推荐”方法,但是您可以使您的操作方法接受一个FormCollection作为参数。然后,您可以编写自己的逻辑来从该对象中提取所需的任何数据。基本上,FormCollection将包含表单中作为键-值对发布的所有字段。
The signature would look like this:
签名如下:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
// logic here to
}
#2
1
Not shure about the "random data" - but maybe the question is realy about "views that are identical"
并不是对“随机数据”羞于启齿——但也许问题的关键在于“相同的视图”
Put the common parts into a partial and the differing parts into the page, if your layout allows it.
如果布局允许的话,把公共部分放到部分中,把不同的部分放到页面中。
You would have a couple of pages then, but no duplicate code.
您将有几个页面,但没有重复的代码。
Or is your problem more on the controller/model side?
还是你的问题更多地在控制器/模型方面?