asp.net mvc 3 c# post array of variables

后端 未结 4 978
我在风中等你
我在风中等你 2020-12-11 18:43

I need to pass array to POST method. But i\'m obviously missing sometging My view look something like this:

<%@ Page Title=\"\" Language=\"C#\" MasterPage         


        
4条回答
  •  失恋的感觉
    2020-12-11 19:11

    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.
    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 (i.e. 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>. Now your view can pass an IEnumerable, which is what your controller action expects.

    Second, change this:

    
    

    To this:

    
    

    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. This obviously isn't quite right, as an IEnumerable exposes no property x. What you want is to bind Model[0].x, which is the property x of the Rezultat object at index 0.

    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)
    }
    

提交回复
热议问题