我是asp.net和MVC的新手,但是我遇到了问题。
我知道这很简单,但是我不知道该怎么做。我寻求建议,并在此先感谢您的帮助。
这是我的问题:
我有2个表:表X:ID(主键),Number;表Y:ID(主键),NID(与表X有关系的外键)等。
我想知道的是如何在HTML编辑器的NID上将最后插入的ID显示到表Y的视图中,以显示ID的最后一个值(表X)?
例如,我在表X中创建一个新行,而当我想在表Y中创建与表X相对应的行以自动获取插入到文本框或编辑器中的最后一个ID时?
谁能给我一些参考或例子!感谢您的帮助!对不起,您的拼写不正确。
开始了 。我对此进行了测试,并向我返回了模型属性以及已发布的文件。这个示例向您提供了如何在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实体,我也在Views上使用相同的实体
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; }
}
-最终将cshml文件。导入点是在BeginForm中使用new {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>
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句