我正在asp.net MVC中使用带有多个文件选择选项的文件上传控件。 当我上传2项内容时,没有任何错误,可以完美运行。 但是,当我没有选择图像时,请在控制器视图中给我计数1。 怎么可能??
这是我的模态
public class DieModels
{
public MouldDetail MD { get; set; }
public MouldSubData MSD { get; set; }
public HttpPostedFileBase DieImage { get; set; }
public List<HttpPostedFileBase> DieDrawing { get; set; }
}
这是我的HTML代码
<div class="col-lg-8" style="margin:0 auto">
<div class="card card-wizard" data-color="rose" id="wizardProfile">
@using (Html.BeginForm("AddNewDie", "Die", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@* @Html.HiddenFor(model => model.OA.ID)*@
<div class="card-header">
<div class="row">
<div class="col-md-1">
<a class="btn btn-warning btn-fill btn-round btn-fab" name="back" href="@Url.Action("DieDetails", "Die")">
<i class="material-icons">backspace</i>
</a>
</div>
<div class="col-md-11">
<h3 class="card-title text-center">
Die Detail
</h3>
<h5 class="card-description text-center">New</h5>
</div>
</div>
</div>
<div class="wizard-navigation">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#step1" data-toggle="tab" role="tab">
Detail
</a>
</li>
<li class="nav-item">
<a class="nav-link redirect" href="#step2" data-toggle="tab" role="tab">
Data Profile
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#step3" data-toggle="tab" role="tab">
Attachments
</a>
</li>
</ul>
</div>
<div class="card-body">
<div class="tab-content">
<div class="tab-pane active" id="step1">
<div class="row justify-content-center">
<div class="col-lg-10">
<div class="form-group">
@Html.HiddenFor(model => model.MD.PCODE, new { @Value = Session["pcode"].ToString() })
</div>
</div>
<div class="col-lg-10">
<div class="form-group require">
@Html.EditorFor(model => model.MD.MOULDCODE, new { htmlAttributes = new { @class = "form-control", @placeholder = "Die Number", Title = "Die Number" } })
@Html.ValidationMessageFor(model => model.MD.MOULDCODE, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group require">
@Html.EditorFor(model => model.MD.CastingDie, new { htmlAttributes = new { @class = "form-control", @placeholder = "Casting Die No ", Title = "Casting Die No" } })
@Html.ValidationMessageFor(model => model.MD.CastingDie, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-6">
<div class="border-checkbox-section">
<div class="border-checkbox-group border-checkbox-group-default" style="display:inline-block">
@Html.CheckBox("CustProperty", (Model != null && Model.MD.CustProperty.HasValue) ? Model.MD.CustProperty : false)
@Html.ValidationMessageFor(model => model.MD.CustProperty, "", new { @class = "text-danger" })
</div>
<label for="" data-toggle="tooltip" title="Checked if this is customer property.">Is Customer Property.?</label>
</div>
</div>
</div>
</div>
<div class="col-lg-10">
<div class="form-group require">
@Html.EditorFor(model => model.MD.PARTNO, new { htmlAttributes = new { @class = "form-control", @placeholder = "Part No. ", Title = "Part No." } })
@Html.ValidationMessageFor(model => model.MD.PARTNO, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-10">
<div class="form-group require">
@Html.EditorFor(model => model.MD.DESCRIPTION, new { htmlAttributes = new { @class = "form-control", @placeholder = "Description ", Title = "Description" } })
@Html.ValidationMessageFor(model => model.MD.DESCRIPTION, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group require">
@Html.EditorFor(model => model.MD.DRG, new { htmlAttributes = new { @class = "form-control", @placeholder = "Drawing No. ", Title = "Drawing No." } })
@Html.ValidationMessageFor(model => model.MD.DRG, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
@Html.EditorFor(model => model.MD.Rev, new { htmlAttributes = new { @class = "form-control", @placeholder = "Drawing Rev. ", Title = "Drawing Rev." } })
@Html.ValidationMessageFor(model => model.MD.Rev, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
@Html.EditorFor(model => model.MD.WEIGHT, new { htmlAttributes = new { @class = "form-control", @placeholder = "Weight", Title = "Weight" } })
@Html.ValidationMessageFor(model => model.MD.WEIGHT, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MD.CastingDrg, new { htmlAttributes = new { @class = "form-control", @placeholder = "Casting Drawing No. ", Title = "Casting Drawing No." } })
@Html.ValidationMessageFor(model => model.MD.CastingDrg, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
@Html.EditorFor(model => model.MD.CastingDrgRev, new { htmlAttributes = new { @class = "form-control", @placeholder = "Casting Drg. Rev. ", Title = "Casting Drawing Rev." } })
@Html.ValidationMessageFor(model => model.MD.CastingDrgRev, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
@Html.EditorFor(model => model.MD.CASTWT, new { htmlAttributes = new { @class = "form-control", @placeholder = "Casting Weight", Title = "Casting Weight" } })
@Html.ValidationMessageFor(model => model.MD.CASTWT, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MD.Ref_Drg, new { htmlAttributes = new { @class = "form-control", @placeholder = "Other Drawing ", Title = "Other Drawing" } })
@Html.ValidationMessageFor(model => model.MD.Ref_Drg, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MD.Ref_DrgRev, new { htmlAttributes = new { @class = "form-control", @placeholder = "Other Drawing Rev.", Title = "Other Drawing Rev." } })
@Html.ValidationMessageFor(model => model.MD.Ref_DrgRev, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-lg-10">
<div class="form-group">
@Html.DropDownList("SupplyCondition", Session["SC"] as SelectList, "-- Select Supply Condition -- ", htmlAttributes: new { @class = "form-control js-example-basic-single" })
@Html.ValidationMessageFor(model => model.MD.SupplyCondition, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-10">
<div class="form-group">
@Html.EditorFor(model => model.MD.SpInstruction, new { htmlAttributes = new { @class = "form-control", @placeholder = "Special Instruction", Title = "Special Instruction" } })
@Html.ValidationMessageFor(model => model.MD.SpInstruction, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="tab-pane" id="step2">
<div class="row justify-content-center">
<div class="col-lg-10">
<div class="form-group">
@Html.DropDownList("MetalCode", Session["metal"] as SelectList, "-- Select Metal -- ", htmlAttributes: new { @class = "form-control js-example-basic-single" })
@Html.ValidationMessageFor(model => model.MSD.MetalCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-10">
<div class="form-group">
@Html.EditorFor(model => model.MSD.MarkingOnParts, new { htmlAttributes = new { @class = "form-control", @placeholder = "Marking On Parts", Title = "Marking On Parts" } })
@Html.ValidationMessageFor(model => model.MSD.MarkingOnParts, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MD.TotalPIN, new { htmlAttributes = new { @class = "form-control", @placeholder = "Total Pin", Title = "Total Pin" } })
@Html.ValidationMessageFor(model => model.MD.TotalPIN, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MD.PINRemarks, new { htmlAttributes = new { @class = "form-control", @placeholder = "Pin Remarks", Title = "Pin Remarks" } })
@Html.ValidationMessageFor(model => model.MD.PINRemarks, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MSD.MetalPinMarking, new { htmlAttributes = new { @class = "form-control", @placeholder = "Metal Marking Pin", Title = "Metal Marketing Pin" } })
@Html.ValidationMessageFor(model => model.MSD.MetalPinMarking, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MSD.CastPartNo, new { htmlAttributes = new { @class = "form-control", @placeholder = "Casting Part No.", Title = "Casting Part No." } })
@Html.ValidationMessageFor(model => model.MSD.CastPartNo, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MSD.PartnoPinMarking, new { htmlAttributes = new { @class = "form-control", @placeholder = "Partno Pin Marking", Title = "Partno Pin Marking" } })
@Html.ValidationMessageFor(model => model.MSD.PartnoPinMarking, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MSD.MouldAttachmentDesc, new { htmlAttributes = new { @class = "form-control", @placeholder = "Extra Attachment", Title = "Mould Attachment Description" } })
@Html.ValidationMessageFor(model => model.MSD.MouldAttachmentDesc, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MD.REMARKS, new { htmlAttributes = new { @class = "form-control", @placeholder = "Remarks", Title = "Remarks" } })
@Html.ValidationMessageFor(model => model.MD.REMARKS, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.DropDownList("SCODE", Session["Supplier"] as SelectList, "-- Select Supplier -- ", htmlAttributes: new { @class = "form-control js-example-basic-single" })
@Html.ValidationMessageFor(model => model.MD.SCODE, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MD.MOULDCAVITY, new { htmlAttributes = new { @class = "form-control", @placeholder = "Total Cavity", Title = "Mould Cavity" } })
@Html.ValidationMessageFor(model => model.MD.MOULDCAVITY, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.DropDownList("MOULDMETAL", Session["DMetal"] as SelectList, "-- Select Die Metal -- ", htmlAttributes: new { @class = "form-control js-example-basic-single" })
@Html.ValidationMessageFor(model => model.MD.MOULDMETAL, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-lg-10">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
@Html.DropDownList("MouldTypeID", Session["DType"] as SelectList, "-- Select Die Type -- ", htmlAttributes: new { @class = "form-control js-example-basic-single" })
@Html.ValidationMessageFor(model => model.MSD.MouldTypeID, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.EditorFor(model => model.MSD.DieContraction, new { htmlAttributes = new { @class = "form-control", @placeholder = "Die Contraction(%)", Title = "Die Contraction(%)" } })
@Html.ValidationMessageFor(model => model.MSD.DieContraction, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane" id="step3">
<div class="row justify-content-center">
<div class="col-lg-6 text-center">
<div class="form-group">
<label><h4>Die Image</h4></label>
<div class="fileinput fileinput-new text-center" data-provides="fileinput">
<div class="fileinput-new thumbnail">
<img src="~/Images/image_placeholder.jpg" alt="...">
</div>
<div class="fileinput-preview fileinput-exists thumbnail"></div>
<div>
<span class="btn btn-rose btn-round btn-file">
<span class="fileinput-new">Select Die</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="DieImage" value="DieImage" accept=".jpg,.png,.JPG,.jpeg,.JPEG,.PNG" />
</span>
<a href="#pablo" class="btn btn-danger btn-round fileinput-exists" data-dismiss="fileinput"><i class="fa fa-times"></i> Remove</a>
</div>
</div>
</div>
</div>
<div class="col-lg-6 text-center">
<div class="form-group">
<label><h4>Drawing Images</h4></label>
<div class="fileinput fileinput-new text-center" data-provides="fileinput">
<div class="fileinput-new thumbnail">
<img src="~/Images/image_placeholder.jpg" alt="...">
</div>
<div class="fileinput-preview fileinput-exists thumbnail"></div>
<div>
<span class="btn btn-rose btn-round btn-file">
<span class="fileinput-new">Select Drawing</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="DieDrawing" value="DieDrawing" accept=".jpg,.png,.JPG,.jpeg,.JPEG,.PNG,.pdf,.PDF" multiple="multiple"/>
</span>
<a href="#pablo" class="btn btn-danger btn-round fileinput-exists" data-dismiss="fileinput"><i class="fa fa-times"></i> Remove</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="card-footer">
<div class="mr-auto">
<input type="button" class="btn btn-previous btn-fill btn-default btn-wd disabled" name="previous" value="Previous">
</div>
<div class="ml-auto">
<input type="button" class="btn btn-next btn-fill btn-rose btn-wd" name="next" value="Next">
<input type="submit" class="btn btn-finish btn-fill btn-rose btn-wd" name="finish" value="Finish" style="display: none;">
</div>
<div class="clearfix"></div>
</div>
</div>
}
</div>
</div>
这是我的控制器代码
if (dm.DieImage != null)
{
string DieNo = dm.MD.MOULDCODE;
string FileName = Path.GetFileNameWithoutExtension(dm.DieImage.FileName);
string Extension = Path.GetExtension(dm.DieImage.FileName);
FileName = DieNo /*+ DateTime.Now.ToString("yymmssfff")*/ + Extension;
//dm.ImagePath = "~/Image/" + FileName;
FileName = Path.Combine(Server.MapPath("~/Images/Die"), FileName);
dm.DieImage.SaveAs(FileName);
}
if (dm.DieDrawing.Count() > 0)
{
for (var i = 0; i < dm.DieDrawing.Count(); i++)
{
string Drg = dm.MD.DRG;
string FName = Path.GetFileNameWithoutExtension(dm.DieDrawing[i].FileName);
string Ext = Path.GetExtension(dm.DieDrawing[i].FileName);
FName = Drg + i /*+ DateTime.Now.ToString("yymmssfff")*/ + Ext;
//dm.ImagePath = "~/Image/" + FileName;
FName = Path.Combine(Server.MapPath("~/Images/Drawing"), FName);
dm.DieDrawing[i].SaveAs(FName);
}
}
答案 0 :(得分:0)
我刚刚为此创建了一个小型测试项目,并且可以确认这是使用List<HttpPostedFileBase>
进行上传时的默认行为。但是,如果您检查元素,则在不上传任何内容时会发现第一个元素是null
。因此,要解决此问题,我只需替换您的代码
if (dm.DieDrawing.Count() > 0)
{
for (var i = 0; i < dm.DieDrawing.Count(); i++)
{
使用
if (dm.DieDrawing != null)
{
var i = 0;
foreach (var file in dm.DieDrawing.Where(f => f != null))
{
i++;
// process file here
}
}