[英]asp.net mvc 3 c# post array of variables
I need to pass array to POST method. 我需要将数组传递给POST方法。 But i'm obviously missing sometging My view look something like this:
但我显然缺少某些东西我的观点看起来像这样:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Klausimynas.Models.Rezultat>" %>
<input type="text" name="x[1]">
<input type="text" name="x[2]">
<input type="text" name="x[3]">
<input type="text" name="x[4]">
<input type="text" name="x[5]">
<input type="text" name="x[6]">
<input type="text" name="x[7]">
My method declaration looks like this: 我的方法声明如下所示:
[HttpPost]
public ActionResult LetsTest(IEnumerable<Rezultat> rez)
and when i'm trying to extract data i'm getting Value can't be null. 当我试图提取数据时,我得到的值不能为空。 What i'm missing?
我错过了什么?
There are a couple of things wrong here: 这里有一些问题:
Rezultat
but you're trying to treat the model as an IEnumerable<Rezultat>
. Rezultat
键入的,但您尝试将模型视为IEnumerable<Rezultat>
。 x[i]
- which would be equivalent to Model.x[i]
- when what you really want is to bind it to [i].x
(ie Model[i].x
). x[i]
- 这相当于Model.x[i]
- 当你真正想要的是将它绑定到[i].x
(即Model[i].x
)时。 So, to correct this, you need to change a couple of things. 所以,要纠正这个问题,你需要改变一些事情。
First, change your view to inherit System.Web.Mvc.ViewPage<IEnumerable<Klausimynas.Models.Rezultat>>
. 首先,将视图更改为继承
System.Web.Mvc.ViewPage<IEnumerable<Klausimynas.Models.Rezultat>>
。 Now your view can pass an IEnumerable<Rezultat>
, which is what your controller action expects. 现在,您的视图可以传递
IEnumerable<Rezultat>
,这是您的控制器操作所期望的。
Second, change this: 第二,改变这个:
<input type="text" name="x[0]">
To this: 对此:
<input type="text" name="[0].x">
The reason for this is that the first will attempt to bind the value to Model.x[0]
, which is (or will be, once you've typed your view properly) equivalent to the first element in property x
of an instance of IEnumerable<Rezultat>
. 原因是第一个会尝试将值绑定到
Model.x[0]
,这是(或者,一旦你正确键入你的视图),相当于一个实例的属性x
中的第一个元素IEnumerable<Rezultat>
。 This obviously isn't quite right, as an IEnumerable
exposes no property x
. 这显然不太正确,因为
IEnumerable
没有公开属性x
。 What you want is to bind Model[0].x
, which is the property x
of the Rezultat
object at index 0
. 你想要的是绑定
Model[0].x
,它是索引0
处的Rezultat
对象的属性x
。
Better still, use a helper to generate the name for you: 更好的是,使用帮助器为您生成名称:
for(int i=0; i < Model.Count; i++)
{
@Html.TextBoxFor(m => m[i].x)
}
if you really wnat to do it this way you have to use I think Form Collection 如果你真的想要这样做,你必须使用我认为Form Collection
[HttpPost]
public ActionResult LetsTest(FormCollection collection, IEnumerable<Rezultat> rez)
{
string[] inputs = new string[6];
for(int i=1; i<8; i++)
{
//get all your array inputs
inputs[i-1]=collection["x["+i+"]"]
}
}
Change your view code as follows: 更改您的视图代码如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Klausimynas.Models.Rezultat>" %>
<input type="text" name="x[0]" />
<input type="text" name="x[1]" />
<input type="text" name="x[2]" />
<input type="text" name="x[3]" />
<input type="text" name="x[4]" />
<input type="text" name="x[5]" />
<input type="text" name="x[6]" />
Update : On 2nd thoughts, I think this is your actual problem, you are starting from 1-7, rather than 0-6. 更新 :第二个想法,我认为这是你的实际问题,你是从1-7开始,而不是0-6。
Thanks Ant. 谢谢蚂蚁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.