2017-01-26 52 views
-1

我是新來的asp.net和mvc,我有一個問題。
我知道這很簡單,但我不知道該怎麼做。我尋求建議,並想提前感謝您的任何幫助。如何在與mvc中另一個表的外鍵關係的html文本框中顯示錶的最後一個插入的ID(主鍵)

這是我的問題:
我有2個表:表X:ID(主鍵),數字;和表Y:ID(主鍵),NID(與表X有關係的外鍵)等。

我想知道的是如何在Html編輯器中將最後插入的ID顯示到表Y的視圖中NID是ID的最後一個值(表X)?
例如,我在表X中創建一個新行,並且當我想在表Y中創建與表X相對應的行時,自動獲取插入到文本框或編輯器中的最後一個ID?

有人可以給我一些參考或例子!感謝您的幫助!對不起,拼寫錯誤。

+0

請問您能證明您嘗試了什麼。示例代碼 –

+0

這是它的一小部分var lastID =(從db.tbl_NIR中的x選擇x.ID).Max(); –

+0

這是來自它的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); –

回答

1

我們走吧。我測試了這一點,它將模型屬性和發佈的文件一起返回給我。這個例子給你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> 
} 
+0

非常感謝,但我真正想知道的是如何獲取當我嘗試在表中創建新行時顯示的表X的主鍵的最後一個值Y作爲外鍵。 –

+0

像這樣 - table X:ID = 2,Number = 100;而我想要的是從表X ID的值2到創建新視圖的表Y的NID的文本框? –

+0

但無論如何謝謝你,我從你的帖子中學到了一些新東西。 –

相關問題