ここに移動します。私はこれをテストし、投稿されたファイルと共にモデルのプロパティを返しました。この例では、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);
}
}
- これらは、MY 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 =(xはdb.tbl_NIRで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"] = "正常に作成されました!"; リターンRedirectToAction( "Index"); } // var lastID =(xはdb.tbl_NIRでx.IDを選択).Max(); //ViewBag.NID =新しいSelectList(db.tbl_NIR、 "ID"、 "NirID"、oi.tbl_NIR.ID); 戻るビュー(oi); –