简体   繁体   English

asp.net mvc 3 c#post变量数组

[英]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: 这里有一些问题:

  1. Your view is typed to Rezultat but you're trying to treat the model as an IEnumerable<Rezultat> . 您的视图是为Rezultat键入的,但您尝试将模型视为IEnumerable<Rezultat>
  2. You're trying to bind each textbox to 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+"]"]
   }

}

Check out model binding to a list here . 在这里查看模型绑定到列表。 Items posted needs to be in certain format to be able to retrieve them as list in POST action. 发布的项目需要采用特定格式才能在POST操作中将其作为列表进行检索。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM