尝试上传图像时,HttpPostfile返回null

时间:2016-02-02 21:15:19

标签: c# html asp.net asp.net-mvc upload

我正在尝试在mvc中上传图像但是它一直返回null。

控制器:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult PostRollOutForm([Bind(Include = "TankSerNo,NozzleSerNo,NozzleLocationDescript,NozzleIdentifier,NozzleFunction,NozzleType,NozzleDiameterorHeight,NozzleWidth,NozzleLocation,NozzleHeight,NozzleWeldSize,NozzleThickness,NozzleComments,RepadShape,RepadHeight,RepadWidth,RepadWeldSize,RepadThickness,TelTale,WeldSpacetoCornerWeld,WeldSpacetoVerticalWeld,WeldSpacetoAdjacentNozzle,WeldSpacetoHorizontalWeld,WeldDetail,CoverThickness,LengthToFlange,LenghtToCtrofValve,CenterLineorLowerHeight,DrawCircleorNot,CornerRadius,Dimension,Label,NozzleThicknessT,NozzleThicknessR,NozzleThicknessB,NozzleThicknessL,Photo")] RollOutViewModel rollout, HttpPostedFileBase uploadedfile, string FailURL)
    {
        if (ModelState.IsValid)
        {
            //upload image attempt 1 
            if(uploadedfile != null) { 
                RollOutFileUploadService service = new RollOutFileUploadService();
                service.SaveFileDetails(uploadedfile, rollout.NozzleSerNo);
            }
            //attempt 2                
            else if (rollout.Photo != null)
            {
                RollOutFileUploadService service = new RollOutFileUploadService();
                service.SaveFileDetails(uploadedfile, rollout.NozzleSerNo);
            }
            //form fields
            ShellRollOut result = new ShellRollOut();
            result.TankSerNo = rollout.TankSerNo;
            result.NozzleSerNo = rollout.NozzleSerNo;
            result.NozzleLocationDescript = rollout.NozzleLocationDescript;
            result.NozzleIdentifier = rollout.NozzleIdentifier;
            result.NozzleFunction = rollout.NozzleFunction;
            result.NozzleType = rollout.NozzleType;
            result.NozzleDiameterorHeight = rollout.NozzleDiameterorHeight;
            result.NozzleWidth = rollout.NozzleWidth;
            result.NozzleLocation = rollout.NozzleLocation;
            result.NozzleHeight = rollout.NozzleHeight;
            result.NozzleWeldSize = rollout.NozzleWeldSize;
            result.NozzleThickness = rollout.NozzleThickness;
            result.NozzleComments = rollout.NozzleComments;
            result.RepadShape = rollout.RepadShape;
            result.RepadHeight = rollout.RepadHeight;
            result.RepadWidth = rollout.RepadWidth;
            result.RepadWeldSize = rollout.RepadWeldSize;
            result.RepadThickness = rollout.RepadThickness;
            result.TelTale = rollout.TelTale;
            result.WeldSpacetoCornerWeld = rollout.WeldSpacetoCornerWeld;
            result.WeldSpacetoVerticalWeld = rollout.WeldSpacetoVerticalWeld;
            result.WeldSpacetoAdjacentNozzle = rollout.WeldSpacetoAdjacentNozzle;
            result.WeldSpacetoHorizontalWeld = rollout.WeldSpacetoHorizontalWeld;
            result.CoverThickness = rollout.WeldSpacetoHorizontalWeld;
            result.WeldDetail = rollout.WeldDetail;
            result.LengthToFlange = rollout.LengthToFlange;
            result.LenghtToCtrofValve = rollout.LenghtToCtrofValve;
            result.CenterLineorLowerHeight = rollout.CenterLineorLowerHeight;
            result.DrawCircleorNot = rollout.DrawCircleorNot;
            result.CornerRadius = rollout.CornerRadius;
            result.Dimension = rollout.Dimension;
            result.Label = rollout.Label;
            result.NozzleThicknessT = rollout.NozzleThicknessT;
            result.NozzleThicknessR = rollout.NozzleThicknessR;
            result.NozzleThicknessB = rollout.NozzleThicknessB;
            result.NozzleThicknessL = rollout.NozzleThicknessL;
            result.Tank = rollout.Tank;

            db.ShellRollOuts.Add(result);
            db.SaveChanges();

            string url = Url.Action("ShellRollOut", new { TankSerNo = rollout.TankSerNo });
            return Json(new { success = true, url = url }, JsonRequestBehavior.AllowGet);

        }
        return PartialView(FailURL, rollout);
    }

查看型号:

   public class RollOutViewModel
{
    public Nullable<int> TankSerNo { get; set; }
    public int NozzleSerNo { get; set; }
    public string NozzleLocationDescript { get; set; }
    public string NozzleIdentifier { get; set; }
    public string NozzleFunction { get; set; }
    public string NozzleType { get; set; }
    public Nullable<float> NozzleDiameterorHeight { get; set; }
    public Nullable<float> NozzleWidth { get; set; }
    public Nullable<float> NozzleLocation { get; set; }
    public Nullable<float> NozzleHeight { get; set; }
    public Nullable<float> NozzleWeldSize { get; set; }
    public Nullable<float> NozzleThickness { get; set; }
    public string NozzleComments { get; set; }
    public string RepadShape { get; set; }
    public Nullable<float> RepadHeight { get; set; }
    public Nullable<float> RepadWidth { get; set; }
    public Nullable<float> RepadWeldSize { get; set; }
    public Nullable<float> RepadThickness { get; set; }
    public string TelTale { get; set; }
    public Nullable<float> WeldSpacetoCornerWeld { get; set; }
    public Nullable<float> WeldSpacetoVerticalWeld { get; set; }
    public Nullable<float> WeldSpacetoAdjacentNozzle { get; set; }
    public Nullable<float> WeldSpacetoHorizontalWeld { get; set; }
    public string WeldDetail { get; set; }
    public Nullable<float> CoverThickness { get; set; }
    public string LengthToFlange { get; set; }
    public string LenghtToCtrofValve { get; set; }
    public string CenterLineorLowerHeight { get; set; }
    public string DrawCircleorNot { get; set; }
    public Nullable<float> CornerRadius { get; set; }
    public string Dimension { get; set; }
    public string Label { get; set; }
    public Nullable<float> NozzleThicknessT { get; set; }
    public Nullable<float> NozzleThicknessR { get; set; }
    public Nullable<float> NozzleThicknessB { get; set; }
    public Nullable<float> NozzleThicknessL { get; set; }

    public HttpPostedFileBase Photo { get; set; }
    public virtual Tank Tank { get; set; }

}

查看:

@using (Html.BeginForm("PostRollOutForm", "Tanks", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="modal-body">
    <div class="form-horizontal">
        @Html.HiddenFor(x => x.NozzleIdentifier, new { @Value = "" })
        @Html.HiddenFor(x => x.TankSerNo)
        @Html.HiddenFor(x => x.Tank)
        @Html.HiddenFor(x => x.NozzleSerNo)
        <h4>Nozzle Data</h4>
        <div class="form-group">
            @Html.LabelFor(model => model.NozzleFunction, "Nozzle Function", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.NozzleFunction, new List<SelectListItem>
                        {
                            new SelectListItem { Text =  "Spiral Stairway", Value = "Spiral Stairway", Selected = true},
                            new SelectListItem { Text =  "Cat Walk", Value = "Cat Walk"},
                            new SelectListItem { Text =  "Vertical Ladder", Value = "Vertical Ladder"},
                            new SelectListItem { Text =  "Platform", Value = "Platform"},
                            new SelectListItem { Text =  "Radial Stairway", Value = "Radial Stairway"},
                        },
             new { @class = "form-control " })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NozzleDiameterorHeight, "Hand Rail Height", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.NozzleDiameterorHeight, new { htmlAttributes = new { @id = "HandRailHeight", @class = "form-control " } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NozzleHeight, "Height Step or Rise", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.NozzleHeight, new { htmlAttributes = new { @id = "HeightStep", @class = "form-control " } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NozzleWidth, "Width of Run", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.NozzleWidth, new { htmlAttributes = new { @id = "WidthRun", @class = "form-control " } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RepadHeight, "Last Step", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.RepadHeight, new { htmlAttributes = new { @id = "RepadHeight", @class = "form-control " } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RepadWidth, "Cage Height", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.RepadWidth, new { htmlAttributes = new { @id = "RepadWidth", @class = "form-control " } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RepadShape, "Shape of Access", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.RepadShape, new List<SelectListItem>
                        {
                            new SelectListItem { Text =  "SW+", Value = "SW+", Selected = true},
                            new SelectListItem { Text =  "SW-", Value = "SW-"},
                            new SelectListItem { Text =  "CATWALK", Value = "CATWALK"},
                            new SelectListItem { Text =  "CAGED LADDER", Value = "CAGED LADDER"},
                            new SelectListItem { Text =  "LADDER", Value = "LADDER"},

                        }, 
             new { @class = "form-control " })
            </div>
        </div>

        <h4>Miscellaneous</h4>

        <div class="form-group">
            @Html.LabelFor(model => model.DrawCircleorNot, "Draw Pipe Circle?", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.DrawCircleorNot, new List<SelectListItem>
                        {
                            new SelectListItem { Text =  "CIRCLE", Value = "CIRCLE"},
                            new SelectListItem { Text =  "NO CIRCLE", Value = "NO CIRCLE", Selected = true},
                        },
             new { @class = "form-control " })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Label, "Label?", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.Label, new List<SelectListItem>
                        {
                            new SelectListItem { Text =  "LABEL", Value = "LABEL", Selected = true},
                            new SelectListItem { Text =  "NO LABEL", Value = "NO LABEL"},
                        },
             new { @class = "form-control " })
            </div>
        </div>

        <hr />
        <div class="form-group">
            @Html.Label("Image of Shell Appurtenance", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.TextBoxFor(a=> a.Photo, new { type = "file", accept = "image/*", capture = "camera", @class = "btn  btn-default btn-sm" })
                <input name="uploadefile" type="file" accept="image/*" capture />
            </div>
        </div>
    </div>
    @Html.HiddenFor(h => h.Tank.Height, new { @id = "TankHeight" })


</div>

据我所知,发布文件的命名约定应该是正确的,所以我不确定为什么它仍然返回null。任何帮助或建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

方法参数名称应与输入名称匹配。在您的代码中,两者都不相同。

所以将剃刀代码更改为

<input name="uploadedfile" type="file"/>