Quantcast
Channel: Quickstep IT » ASP.Net MVC
Viewing all articles
Browse latest Browse all 4

Passing array data between controllers and views in ASP.NET MVC 3

0
0

In ASP.NET MVC 3 model binding array data is not as simple as it seems.  Take this excerpt from one of my project models.

     public class CustomerActionModel
    {
        public CustomerActionModelActionType { get; set; }

        public int[] RecordIDList { get; set; }
    }

In one view my users select a number of records they want to update.  This results in an array list of record IDs.  In another view I ask what operation to perform.  The array of record IDs must be passed to this new view, hidden and returned to my controller action.

	@using (Html.BeginForm("CustomerAction", "MyController"))
	{

		@Html.HiddenFor(m => m.RecordIDList)

		@Html.EditorFor(m => m.CustomerActionModelActionType)
	}

Unfortunately, on return to the controller action the array list is a null object.  It seems that there is an issue with MVC 3 model binding array data.  The data does arrive in the view but does not come back.  You can see this in action by looking at the parameter data in your url in the browser or in Fiddler.

The solution is simple and mundane.  Just write out the values in your array into the view.

	@using (Html.BeginForm("CustomerAction", "MyController"))
	{
		@for (var i = 0; i < Model.RecordIDList.Count(); i++)
		{
			@Html.HiddenFor(m => m.RecordIDList[i]);
		}

		@Html.EditorFor(m => m.CustomerActionModelActionType)

	}

Now when you return from your view you will see your RecordIDList data in the URL parameters and they will arrive safe and sound back into your controller action.

Be Sociable, Share!

    The post Passing array data between controllers and views in ASP.NET MVC 3 appeared first on Quickstep IT.


    Viewing all articles
    Browse latest Browse all 4

    Latest Images

    Trending Articles





    Latest Images