Define model the two-dimensional Array type attribute

Asked

Viewed 223 times

1

There is a way to create an attribute of type array two-dimensional no Model so you can use this attribute to mount a table in View.

Example: No model define an attribute of type array two-dimensional (int[,] matriz) and in the controller define that the array will be 20x20 and each position will have value X, for example, and in view will have a foreach traversing the array to mount a table x filled in each cell.

In the Model created attributes with multiple columns and in controller I use a list of the enumerable type with 20 positions, but I didn’t think it was cool because I want the array mount to be more dynamic.

  • 1

    Yes, you can. Enter your code so we can address.

1 answer

0

In the Model created attributes with multiple columns and in controller use a list of type enumerable with 20 positions, but did not find it cool because I want the mounting of the array was more dynamic.

By definition, arrays are not dynamic. If the idea is to be more dynamic, use objects that work well with variable number of indices, such as List<T> or Dictionary<TKey, TValue>.

Still, if you want to work with matrices at the level of View can implement the Modelbinder down below:

public class TwoDimensionalArrayBinder<T> : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        string key = bindingContext.ModelName;
        try
        {
            int totalRows = 0;
            while (bindingContext.ValueProvider.GetValue(key + totalRows) != null)
                totalRows++;
            ValueProviderResult[] val = new ValueProviderResult[totalRows];
            for (int i = 0; i < totalRows; i++)
                val[i] = bindingContext.ValueProvider.GetValue(key + i);
            if (val.Length > 0)
            {
                int totalColumn = ((string[])val[0].RawValue).Length;
                T[,] twoDimensionalArray = new T[totalRows, totalColumn];
                StringBuilder attemptedValue = new StringBuilder();
                for (int i = 0; i < totalRows; i++)
                {
                    for (int j = 0; j < totalColumn; j++)
                    {
                        twoDimensionalArray[i, j] = (T)Convert.ChangeType(((string[])val[i].RawValue)[j], typeof(T));
                        attemptedValue.Append(twoDimensionalArray[i, j]);
                    }
                }
                bindingContext.ModelState.SetModelValue(key, new ValueProviderResult(twoDimensionalArray, attemptedValue.ToString(), CultureInfo.InvariantCulture));
                return twoDimensionalArray;
            }
        }
        catch
        {
            bindingContext.ModelState.AddModelError(key, "Data is not in correct Format");
        }
        return null;
    }
}

And wear it like this:

[HttpPost]
public ActionResult Index([ModelBinder(typeof(TwoDimensionalArrayBinder<int>))] int[,] Matriz
{ ... }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.