0

I have two editor templates:

UploadFiles.cshtml:

@model HttpPostedFileBase[]
@Html.EditorFor(m => Model, "UploadFile", new { multiple = true })

UploadFile.cshtml:

@model HttpPostedFileBase
@Html.TextBox("", Model, new { type = "file", multiple = Convert.ToBoolean(ViewBag.Multiple) ? "multiple" : "" })
<!-- Additional code here i don't wish to repeat in both controls -->

Notice how UploadFiles.cshtml template accepts an array and then calls the UploadFile.cshtml template and passes in multiple = true via the view data.

The problem i have is if i say:

@Html.EditorFor(m => Model.Files, "UploadFiles")

It doesn't render anything.

However if i say:

@Html.EditorFor(m => Model.File, "UploadFile")

It renders correctly.

I'd appreciate if someone could show me how this can be achieved.

Thanks

1 Answer 1

1

In UploadFiles.cshtml, you're passing Model, which is an array, to UploadFile.cshtml, which does not take an array. Did you mean to wrap that line in a foreach?

foreach (var file in Model)
{
    @Html.EditorFor(x => file, "UploadFile", new { multiple = true })
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I can't believe i didn't notice i was passing in the wrong model type. However the array for the files is null and will not render anything. After further research it appears what i'm trying to do won't work as it will always become a partial template of the editor. I will accept your answer as it did point me in the right direction.

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.