Couldn't generate scaffold for file upload

2019-08-08 16:18发布

问题:

I tried to generate a view for file upload but the upload part was missing and the rest class members have view generated for them

View Model

namespace myapps.Models.ViewModels
{
    public class UserPhotoUploadViewModel
    {

        public HttpPostedFile PhotoFile
        {
            get;
            set;
        }

        public Guid UserID
        {
            get;
            set;
        }
    }
}

Generated View

@model myapps.Models.ViewModels.UserPhotoUploadViewModel

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>UserPhotoUploadModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserID)
            @Html.ValidationMessageFor(model => model.UserID)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

Am I expected to fill in the rest? Is this a usual behaviour and are there limit to what asp.net MVC3 could generate?

回答1:

Yes, this is standard behavior. You can examine the default T4 Template and see how it works, it should be located in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\AddView\CSHTML\Create.tt

The most important method is GetEligibleProperties - as you can see it gets only primitive types (that are public and so on).

Here's an example of dealing with HttpPostedFile in ASP.NET MVC projects Can EditorFor() be used to create <input type="file">?