我坚持上传3张图片,请建议我如何继续前进 如果你愿意,我可以给你们发电子邮件或上传我的完整解决方案
下面是一些细节:
我的控制器:
namespace TiresalesClaim.Controllers
{
public class TsiclaimsController : Controller
{
private TiresalesClaimContext db = new TiresalesClaimContext();
// GET: Tsiclaims
public ActionResult Index(string searchBy, string search, int? page)
{
// var claims = db.Tsiclaims.Include(d => d.disposition).ToList();
if (searchBy == "DistributorName")
{
return View(db.Tsiclaims.Where(x => x.DistributorName.Contains(search) || search == null)
.ToList().ToPagedList(page ?? 1, 20));
}
else
if (searchBy == "TyreSize")
{
return View(db.Tsiclaims.Where(x => x.TyreSize.Contains(search) || search == null)
.ToList().ToPagedList(page ?? 1, 20));
}
else
if (searchBy == "TyreBrand")
{
return View(db.Tsiclaims.Where(x => x.TyreBrand.Contains(search) || search == null)
.ToList().ToPagedList(page ?? 1, 20));
}
else
if (searchBy == "Disposition")
{
return View(db.Tsiclaims.Where(x => x.DispositionAsPerTSI.Contains(search) || search == null)
.ToList().ToPagedList(page ?? 1, 20));
}
else
{
return View(db.Tsiclaims.ToList().ToPagedList(page ?? 1, 20));
}
}
public ActionResult OrderByDistributorName(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.DistributorName ascending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
public ActionResult OrderBySize(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.TyreSize ascending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
public ActionResult OrderByBrand(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.TyreBrand ascending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
public ActionResult OrderByDisposition(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.DispositionAsPerTSI descending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
public ActionResult OrderByDate(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.ClaimRecDate descending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
// GET: Tsiclaims/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tsiclaim tsiclaim = db.Tsiclaims.Find(id);
if (tsiclaim == null)
{
return HttpNotFound();
}
return View(tsiclaim);
}
// GET: Tsiclaims/Create
public ActionResult Create()
{
TableDbContext tdb = new TableDbContext();
ViewBag.distributor = new SelectList(tdb.DistributorNames, "Name", "Name");
ViewBag.size = new SelectList(tdb.TyreSizes, "Name", "Name");
ViewBag.brand = new SelectList(tdb.TyreBrands, "Name", "Name");
ViewBag.Dispo = new SelectList(tdb.DispositionAsPerTSIs, "Name", "Name");
return View();
}
// POST: Tsiclaims/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,DistributorName,ClaimRecDate,State,Country,TelNo,FaxNo,Email,TyreSize,TyreBrand,TyreSerialNo,RemainingTreadDept,TreadWear,DefectAsPerDistributor,TypeofTyreWear,DefectAsPerTSI,DispositionAsPerTSI,ClaimValueAwarded,InspectedBy,Picture1,Picture2,Picture3")] Tsiclaim tsiclaim)
{
if (ModelState.IsValid)
{
db.Tsiclaims.Add(tsiclaim);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tsiclaim);
}
// GET: Tsiclaims/Edit/5
public ActionResult Edit(int? id)
{
TableDbContext tdb = new TableDbContext();
ViewBag.distributor = new SelectList(tdb.DistributorNames, "Name", "Name");
ViewBag.size = new SelectList(tdb.TyreSizes, "Name", "Name");
ViewBag.brand = new SelectList(tdb.TyreBrands, "Name", "Name");
ViewBag.Dispo = new SelectList(tdb.DispositionAsPerTSIs, "Name", "Name");
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tsiclaim tsiclaim = db.Tsiclaims.Find(id);
if (tsiclaim == null)
{
return HttpNotFound();
}
return View(tsiclaim);
}
// POST: Tsiclaims/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,DistributorName,ClaimRecDate,State,Country,TelNo,FaxNo,Email,TyreSize,TyreBrand,TyreSerialNo,RemainingTreadDept,TreadWear,DefectAsPerDistributor,TypeofTyreWear,DefectAsPerTSI,DispositionAsPerTSI,ClaimValueAwarded,InspectedBy,Picture1,Picture2,Picture3")] Tsiclaim tsiclaim)
{
if (ModelState.IsValid)
{
db.Entry(tsiclaim).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tsiclaim);
}
// GET: Tsiclaims/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tsiclaim tsiclaim = db.Tsiclaims.Find(id);
if (tsiclaim == null)
{
return HttpNotFound();
}
return View(tsiclaim);
}
// POST: Tsiclaims/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Tsiclaim tsiclaim = db.Tsiclaims.Find(id);
db.Tsiclaims.Remove(tsiclaim);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
我的创建页面:
@model TiresalesClaim.Models.Tsiclaim
@
{
ViewBag.Title = "Create";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@*<h1>Tire Sales International</h1>*@
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@*<div class="form-group">
@Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
</div>
</div>*@
<div class="form-group">
@Html.LabelFor(model => model.DistributorName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.DistributorName, (SelectList)ViewBag.distributor, "Select Distributor")
@*@Html.DropDownList("DistributorName", new List<SelectListItem> {
new SelectListItem{Text="Al Alaly Tyre", Value="Al Alaly Tyre"},
new SelectListItem{Text="Jilphar", Value="Jilphar"}
}, "Select Distributor Name")*@
@*@Html.EditorFor(model => model.DistributorName, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.DistributorName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ClaimRecDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ClaimRecDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ClaimRecDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.State, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Country, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TelNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TelNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TelNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FaxNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FaxNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FaxNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TyreSize, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.TyreSize, (SelectList)ViewBag.size, "Select Tyre Size")
@*@Html.DropDownList("TyreSize", new List<SelectListItem> {
new SelectListItem{Text="GD 1200r24 20pr HF702", Value="GD 1200r24 20pr HF702"},
new SelectListItem{Text="GD 1200r24 20pr GD122", Value="GD 1200r24 20pr GD122"},
new SelectListItem{Text="TF 1200r24 20pr TF801", Value="TF 1200r24 20pr TF801"},
new SelectListItem{Text="TF 1200r24 20pr TF580", Value="TF 1200r24 20pr TF580"},
new SelectListItem{Text="YG 1200r24 20pr FH326", Value="YG 1200r24 20pr FH326"},
new SelectListItem{Text="GD 315/80r22.5 18pr GD660", Value="GD 315/80r22.5 18pr GD660"},
new SelectListItem{Text="GD 315/80r22.5 18pr GD768", Value="GD 315/80r22.5 18pr GD768"},
new SelectListItem{Text="YG 315/80r22.5 18pr FH158", Value="YG 315/80r22.5 18pr FH158"},
new SelectListItem{Text="YG 315/80r22.5 18pr FH159", Value="YG 315/80r22.5 18pr FH159"},
new SelectListItem{Text="GD 385/65r22.5 20pr GD022", Value="GD 385/65r22.5 20pr GD022"},
new SelectListItem{Text="YG 385/65r22.5 20pr FH538", Value="YG 385/65r22.5 20pr FH538"},
}, "Select Tyre Size")*@
@*@Html.EditorFor(model => model.TyreSize, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.TyreSize, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TyreBrand, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.TyreBrand, (SelectList)ViewBag.brand, "Select Tyre Brand")
@*@Html.DropDownList("TyreBrand", new List<SelectListItem> {
new SelectListItem{Text="GreenDragon Brand Tyre", Value="GreenDragon Brand Tyre"},
new SelectListItem{Text="Tuffstone Brand Tyre", Value="Tuffstone Brand Tyre"},
new SelectListItem{Text="Young Brand Tyre", Value="Young Brand Tyre"}
}, "Select Tyre Brand")*@
@*@Html.EditorFor(model => model.TyreBrand, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.TyreBrand, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TyreSerialNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TyreSerialNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TyreSerialNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RemainingTreadDept, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RemainingTreadDept, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RemainingTreadDept, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TreadWear, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TreadWear, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TreadWear, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DefectAsPerDistributor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DefectAsPerDistributor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DefectAsPerDistributor, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TypeofTyreWear, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TypeofTyreWear, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TypeofTyreWear, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DefectAsPerTSI, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DefectAsPerTSI, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DefectAsPerTSI, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DispositionAsPerTSI, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model=>model.DispositionAsPerTSI,(SelectList)ViewBag.Dispo, "Select Disposition")
@*@Html.DropDownList("DispositionAsPerTSI", new List<SelectListItem> {
new SelectListItem{Text="Pass", Value="Pass"},
new SelectListItem{Text="Fail", Value="Fail"}
}, "Select Disposition As Per TSI")*@
@*@Html.EditorFor(model => model.DispositionAsPerTSI, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.DispositionAsPerTSI, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ClaimValueAwarded, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ClaimValueAwarded, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ClaimValueAwarded, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.InspectedBy, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.InspectedBy, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.InspectedBy, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Picture1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Picture1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Picture1, "", new { @class = "text-danger" })
<input type="file" class="form-control" id="Picture1"/>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Picture2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Picture2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Picture2, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Picture3, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Picture3, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Picture3, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@*<input type="submit" value="Create" class="btn btn-default" />*@
<input type="image" src="~/Icons/Save.png" alt="Submit" width="40" height="40">
</div>
</div>
</div>}<div><a href="@Url.Action("Index")"><img src="@Url.Content("~/Icons/go-back-icon.png")" width="50" />Back</a></div>@section Scripts {@Scripts.Render("~/bundles/jqueryval")}
我的模特:
namespace TiresalesClaim.Models
{
public class Tsiclaim
{
[Key]
[DisplayName("Claim Ref#")]
public int Id { get; set; }
//[Key]
//[Range(1,999999)]
//public int Id { get; set; }
[Required]
[DisplayName("Distributor Name")]
public string DistributorName { get; set; }
[DataType(DataType.Date)]
[DisplayName("Claim Rec Date")]
public DateTime ClaimRecDate { get; set; }
[Required]
[DisplayName("State")]
public string State { get; set; }
[Required]
[DisplayName("Country")]
public string Country { get; set; }
[DataType(DataType.PhoneNumber)]
[DisplayName("Tel #")]
public string TelNo { get; set; }
[DisplayName("Fax #")]
[DataType(DataType.PhoneNumber)]
public string FaxNo { get; set; }
[DisplayName("Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DisplayName("Tyre Size")]
public string TyreSize { get; set; }
[Required]
[DisplayName("Tyre Brand")]
public string TyreBrand { get; set; }
//[Required]
//public string PlyRating { get; set; }
[Required]
[DisplayName("Tyre Serial #")]
public string TyreSerialNo { get; set; }
[Required]
[DisplayName("Rmaining Tread Dept")]
public decimal RemainingTreadDept { get; set; }
[Required]
[DisplayName("% Tread Wear")]
public decimal TreadWear { get; set; }
[Required]
[DisplayName("Defect As Per Distributor")]
public string DefectAsPerDistributor { get; set; }
[Required]
[DisplayName("Type of Tyre Wear")]
public string TypeofTyreWear { get; set; }
[Required]
[DisplayName("Defect As Per TSI")]
public string DefectAsPerTSI { get; set; }
[Required]
[DisplayName("Disposition As Per TSI")]
public string DispositionAsPerTSI { get; set; }
[Required]
[DisplayName("% Claim Value Awarded")]
public decimal ClaimValueAwarded { get; set; }
[Required]
[DisplayName("Inspected By")]
public string InspectedBy { get; set; }
[DataType(DataType.Upload)]
public string Picture1 { get; set; }
[DataType(DataType.Upload)]
public string Picture2 { get; set; }
[DataType(DataType.Upload)]
public string Picture3 { get; set; }
[DisplayName("Status")]
public string Status { get; set; }
[DisplayName("Remarks")]
public string Remarks { get; set; }
//[NotMapped]
//public virtual DispositionAsPerTSI disposition { get; set; }
}
}
答案 0 :(得分:0)
您需要从包含文件数据的对象获取文件数据,即HttpPostedFileBase。然后,您可以获取数据和完全限定的名称/路径,并根据需要单独保存。
请参阅此链接以获取示例:Uploading a File (Or Files) With ASP.NET MVC
这是一个简单的例子:
查看:
<form action="" method="post" enctype="multipart/form-data">
<label for="file1">Filename:</label>
<input type="file" name="files" id="file1" />
<label for="file2">Filename:</label>
<input type="file" name="files" id="file2" />
<input type="submit" />
</form>
控制器:
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}