[英]Create Bulk Records in Asp.Net Core 2.1.0 Razor Page
I want to update the default scenario to insert multiple records at a time by doing following. 我想通过执行以下操作来更新默认方案,一次插入多个记录。
On Form Submit, data to be submitted to a List/Table and display on the same page. 在“表单提交”上,要提交到列表/表并显示在同一页面上的数据。 May be using Ajax or server-side.
可能正在使用Ajax或服务器端。
On Final Submit post, insert all records to the DB. 在“最终提交”上,将所有记录插入数据库。
My Entity 我的实体
public class ItemAttribute
{
[Key]
public int ID { get; set; }
public int ItemID { get; set; }
public string UoM { get; set; }
public decimal SaleVal { get; set; }
[ForeignKey("ItemID")]
public Item Item { get; set; }
}
My Create Model 我的创建模型
private readonly Test3.Models.Test3Context _context;
public CreateModel(Test3.Models.Test3Context context)
{ _context = context; }
public IActionResult OnGet()
{
ViewData["ItemID"] = new SelectList(_context.Item, "ItemID", "ItemID");
return Page();
}
[BindProperty]
public ItemAttribute ItemAttribute { get; set; }
public async Task<IActionResult> OnPostAsync()
{
_context.ItemAttribute.Add(ItemAttribute);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
Here is Razor Page 这是剃刀页
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ItemAttribute.ItemID" class="control-label"></label>
<select asp-for="ItemAttribute.ItemID" class ="form-control" asp-items="ViewBag.ItemID"></select>
</div>
<div class="form-group">
<label asp-for="ItemAttribute.UoM" class="control-label"></label>
<input asp-for="ItemAttribute.UoM" class="form-control" />
<span asp-validation-for="ItemAttribute.UoM" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ItemAttribute.SaleVal" class="control-label"></label>
<input asp-for="ItemAttribute.SaleVal" class="form-control" />
<span asp-validation-for="ItemAttribute.SaleVal" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
Here everything works for single record insertion to the DB. 此处,所有操作都适用于将单个记录插入数据库。
Please suggest me the way to achieve this. 请为我提出实现此目标的方法。
There is no any direct way as such. 尚无任何直接方法。
You can use library which will store entire object in client side. 您可以使用将整个对象存储在客户端的库。 ie
Knockout
JS or Angular
JS 即
Knockout
JS或Angular
JS
and in the last post all data to server side 并在最后发布所有数据到服务器端
var data = JSON.stringify({ 'values': values});
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: 'yourURL',
data: data,
success: function () {
$('#result').html('successfully called.');
},
failure: function (response) {
$('#result').html(response);
}
});
and in the controller : 并在控制器中:
public async Task<IActionResult> saveData(List<ItemAttribute> data)
{
foreach(var item in data)
{
_context.ItemAttribute.Add(item);
}
await _context.SaveChangesAsync();
}
I have found a solution using Session
and List<T>
. 我已经找到了使用
Session
和List<T>
的解决方案。
Add one more Input
button to handle the data to store in Session
with asp-page-handler="AddtoList"
再添加一个
Input
按钮,以处理要存储在Session
的数据,方法是使用asp-page-handler="AddtoList"
OnPostAddtoList
method, load already existing session data to List<T>
and append new record to it and save to session back. OnPostAddtoList
方法,将现有的会话数据加载到List<T>
并将新记录追加到它,然后保存到会话中。
Here is the OnPostAddtoList()
这是
OnPostAddtoList()
var key = "ItemList";
List<ItemAttribute> TempList = new List<ItemAttribute>();
var value = HttpContext.Session.GetString(key);
if (value != null)
{ TempList = JsonConvert.DeserializeObject<List<ItemAttribute>>(value); }
var ItemAttribute1 = new ItemAttribute { UoM = ItemAttribute.UoM, SaleVal = ItemAttribute.SaleVal};
TempList.Add(ItemAttribute1);
ItemAttributeList = TempList;
HttpContext.Session.SetString(key, JsonConvert.SerializeObject(ItemAttributeList));
return Page();
.cshtml
and in Session
. .cshtml
和Session
可用。 Repeat the it till your list completes. List<T>
and do DB saving or whatever your want. List<T>
并进行数据库保存或任何您想要的操作。 Note: Following is the code to session to work in Asp.Net Core
注意:以下是在
Asp.Net Core
进行会话的代码
In Startup.cs
, Configuration
method, 在
Startup.cs
, Configuration
方法,
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
});
And in Configure
method, add app.UseSession();
然后在
Configure
方法中,添加app.UseSession();
before app.UseMvc();
在
app.UseMvc();
之前app.UseMvc();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.