开发者

MVC controller returned parameter as null for complex data type

public class FileSubmissionModel
{
    public List<DraftFile> ValidFiles{ get ; set ;  }
    public List<DraftFile> InvalidFiles{ get ; set ;  }
}

Where:

public class DraftFile
{

    public Guid DraftId { get ; set ;  }
    public string OriginalFileName { get ; set ;  }
}

Gives a null on:

[HttpPost]
public ActionResult Step2(FileSubmissionModel validFiles)
{
 ...
}

The parameter 'validFiles.ValidFiles' & 'validFiles.InvalidFiles' are returned as null. Does anyone know why and what I can do to correct this? Cheers, Pete

View code as asked for...I've snipped some out but you get the gist:

@foreach (var item in Model.InvalidFiles)
        {
        count++;
        <tr>
       开发者_JAVA技巧     @Html.HiddenFor(model => model.InvalidFiles[count].DraftId) 
            @Html.HiddenFor(model => model.InvalidFiles[count].OriginalFileName ) 
        </tr>
        }


The problem seems to be that the parameter to the Step2 method validFiles is the same as the ValidFiles property on the FileSubmissionModel.

Assuming that within the view you are doing something like:

@for(var i = 0; i < 10; i++)
{
 <p>  @Html.EditorFor(model => model.ValidFiles[i].DraftId)</p>
}

The name of the field will be generated as ValidFiles[0].DraftId which is what gets posted back to the server.

The default model binder is trying to bind to the parameter rather than to the property on the model because the names are the same and the default model binder does a case insensitive match.

My advice right now is to change the name of the parameter on your method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜