我們走吧。我測試了這一點,它將模型屬性和發佈的文件一起返回給我。這個例子給你ideea如何在MVC中使用POSt方法以及如何將模型屬性發送回控制器。
//-- this is the controller
public class FileUploadDemoController : Controller
{
//
// GET: /FileUploadDemo/
public ActionResult Index()
{
// here find the last if of the FileUploadtable
var ctx = new TestDbContext();
var maxId = ctx.Fileuploads.ToList().OrderByDescending(u => u.Id).FirstOrDefault();
var newId = maxId == null ? 1 : maxId.Id + 1;
return View("Index", new FileUploadModel { Id= newId });
}
[HttpPost]
public ActionResult PostForm(FileUploadModel model)
{
// here you have NewId in model.Id method ; Now ypour table b in my case is fileeuploadhistory I want to insert a new record with this model.Id
using (var ctx = new TestDbContext())
{
var curretFile = ctx.Fileuploads.FirstOrDefault(x => x.Id == model.Id);
if (curretFile==null)
{
curretFile=new FileUploadModel { Name=model.Name , ValidFromDate= model.ValidFromDate};
}
curretFile.History = new FileUploadHistory { InsertedDate = DateTime.Now };
ctx.Fileuploads.Add(curretFile);
ctx.SaveChanges();
}
return View("Index", model);
}
}
- 這些都是我的EntityFramework的實體,我在視圖中使用相同的還有
public class FileUploadModel
{
public FileUploadModel()
{
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string ValidFromDate { get; set; }
public int HistoryId { get; set; }
[ForeignKeyAttribute("HistoryId")]
public virtual FileUploadHistory History { get; set; }
}
public class FileUploadHistory
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime InsertedDate { get; set; }
}
- Finaly的cshml文件。導入點是在BeginForm中使用新的{enctype =「multipart/form-data」}。 //您將從哪裏發佈數據的頁面。請改變你的模型類代替我爲我創建的FileUploadModel。
@model WebApplication1.Models.FileUploadModel
@using (Html.BeginForm("PostForm", "FileUploadDemo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="panel">
<div class="panel-body">
<div class="form-group row">
<div class="col-md-2 form-label">
<label>ID:</label>
</div>
<div class="col-md-6">
@Html.TextAreaFor(x => x.Id , new { @class = "form-control" })
</div>
</div>
<div class="form-group row">
<div class="col-md-2 form-label">
<label>Name:</label>
</div>
<div class="col-md-6">
@Html.TextAreaFor(x => x.Name, new { @class = "form-control" })
</div>
</div>
<div class="form-group row">
<div class="col-md-2 form-label">
<label>Date</label>
</div>
<div class="col-md-6">
@Html.TextAreaFor(x => x.ValidFromDate, new { @class = "form-control" })
</div>
</div>
<div class="col-md-10">
<div class="form-group row">
<div class="col-md-2 form-label">
<label>Select File<i class="required-field">*</i>:</label>
</div>
<div class="col-md-8">
<input type="file" class="file-upload" style="margin: 0px;" hidden="hidden" accept=".xlsx" name="file" id="file" />
</div>
</div>
</div>
<div class="form-group row">
<div class="col-md-3 pull-right text-right">
<button class="btn btn-primary" id="process-submission" type="submit">
Submit
</button>
</div>
</div>
</div>
</div>
}
請問您能證明您嘗試了什麼。示例代碼 –
這是它的一小部分var lastID =(從db.tbl_NIR中的x選擇x.ID).Max(); –
這是來自它的post方法的東西://oi.tbl_NIR.ID = db.tbl_NIR.OrderByDescending(x => x.ID).FirstOrDefault()。ID; db.tbl_I_O.Add(OI); db.SaveChanges(); //oi.NirID = ni.ID; // int lastID = oi.tbl_NIR.ID; //ViewBag.lastID = lastID; TempData [「Msg」] =「創建成功!」; return RedirectToAction(「Index」); } // var lastID =(從db.tbl_NIR中的x選擇x.ID).Max(); //ViewBag.NID = new SelectList(db.tbl_NIR,「ID」,「NirID」,oi.tbl_NIR.ID); return查看(oi); –