2

I am taking an string[] from my Model and it has about 25ish strings in it at any given time.

@model PostProcessPartSelectionViewModel

@{
    var i = 0;
    foreach (var part in Model.PartsAllowedAsSeed)
    {
        <input type="checkbox" id="[@i]" name="PartsAllowedAsSeed" value="@part" />
        <span>@part</span>
        <br />
        i++;
    }
}

I set up a @foreach loop in Razor to display a checkbox and label for each string, but when I debug, @part renders to System.Object[]. There are 25 checkboxes with 25 "System.Object[]" labels.

Eventually, I'm going to want to return any checked strings back to the model, but right now I just want to know how I can get Razor to render the actual string value.

3
  • The value of the items found in the PartsAllowedAsSeed property is an array of objects, so "System.Object[]" is the actual string value. What are you trying to do with the objects found in those arrays? Commented Jul 28, 2014 at 17:43
  • I should have specified this in the question, but the System.Object[] has strings inside it, but for my needs, I just need to encode each object as a single string. Commented Jul 28, 2014 at 17:46
  • So you're saying that the first part in PartsAllowedAsSeed would be something like {"a", "b", "c"}? In that case, what string do you want it to output in your <span>? Commented Jul 28, 2014 at 21:26

2 Answers 2

2

Don't use foreach in razor, use a for loop so you can directly bind to your model:

@for (int i = 0; i < @Model.PartsAllowedAsSeed.Length; i++)
{
    <input type="checkbox" id="[@i]" name="PartsAllowedAsSeed" />
    <span> @Model.PartsAllowedAsSeed[i] </span>
    <br />
}

as for your System.Object[], you can do string.Join(", ", @Model.PartsAllowedAsSeed[i]) or some equivalent to meet your needs

Sign up to request clarification or add additional context in comments.

1 Comment

I think that in the long run, it is much better for me to use a for instead of a foreach. However, I don't think that string.Join is going to do the trick for me, as it still returns System.Object[].
0

My case was pretty specific, so I don't think this will apply to anyone. I had a Object[] with objects in it and I wanted to display each child object as a string with Razor. I used a hack to cast the Object[] to a list, then append brackets to each list entry and reported each string back to Razor. For now, I don't need to bind directly to the model, so I opted to just use a foreach.

Razor Code:

@{
    var i = 0;
    @foreach (var item in Model.PartsAllowedAsSeed)
    {
        <input type="checkbox" id="@i" name="PartsAllowedAsSeed" />
        <span>@Html.ConvertToArray(item)</span>
        <br />
        i++;
    }
}

Helper Class:

public static MvcHtmlString ConvertToArray(this HtmlHelper htmlHelper, object source)
        {
            var src = source as IEnumerable;

            if (src == null) return MvcHtmlString.Create(string.Empty);

            var sourceAsList = src.Cast<Object>().ToList();

            var output = new StringBuilder();
            output.Append("[");
            for (var index = 0; index < sourceAsList.Count; index++)
            {
                var item = sourceAsList[index];
                output.Append(item);
                if (index != (sourceAsList.Count - 1)) output.Append(", ");
            }
            output.Append("]");

            return MvcHtmlString.Create(output.ToString());
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.