首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpPostfile在试图上载图像时返回null

HttpPostfile在试图上载图像时返回null
EN

Stack Overflow用户
提问于 2016-02-02 21:15:18
回答 1查看 39关注 0票数 0

我正在尝试上传mvc中的图像,但是它始终返回null。

主计长:

代码语言:javascript
复制
    [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);
    }

视图模型:

代码语言:javascript
复制
   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; }

}

查看:

代码语言:javascript
复制
@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。任何帮助或建议都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-02 21:19:37

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

因此,将剃须刀代码更改为

代码语言:javascript
复制
<input name="uploadedfile" type="file"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35163920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档