![](/img/trans.png)
[英]ASP.Net Core MVC - Run client side script/function after form submit data success
[英]Populate Form Fields after Submit ASP.NET Core 3.1 MVC
在提交表單(使用 HttpPost)后,我正在嘗試重新填充我的輸入字段。 有沒有一種簡單的方法可以做到這一點? 我有一個下拉列表和一個文本框,其中填充了數據庫中的數據。 它們在處理數據流的 controller 中都有自己的功能。 我的目標是在提交后讓最后創建的數據出現在輸入字段中。
我的 Model
public int ID { get; set; }
public string bookName { get; set; }
public string Author { get; set; }
我的觀點
<form method="post" asp-controller="Home" asp-action="Home" role="post">
<div class="form-group">
<label asp-for="bookName"></label>
<select name="bookName" asp-items="@(new SelectList(ViewBag.message, "ID", "bookName"))">
</select>
</div>
<div class="form-group">
<label asp-for="Author"></label>
<input asp-for="Author" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</form>
我的 Controller
public void GetBooksDDL()
{
List<BookModel> bookName = new List<BookModel>();
bookName = (from b in _context.BookModel select b).ToList();
bookName.Insert(0, new BookModel { ID = 0, bookName = "" });
ViewBag.message = bookName;
}
[HttpPost("[action]")]
[Route("/Home")]
[Produces("application/json")]
public async Task<IActionResult> Home()
{
if (textbox != "")
{
//do all the submit actions
//after all of the actions are complete return the view:
GetBooksDDL();
return View();
}else
{
return Error;
}
}
我知道我可以在 View() 中傳入 model,但值始終是 null。 我試圖將我的(BookModel 模型)作為 HttpPost 中的參數傳遞,但我得到了 415 狀態。
在提交表單(使用 HttpPost)后,我正在嘗試重新填充我的輸入字段。
對於簡單的輸入字段,只需return View(model)
。對於SelectList
,您需要重新設置值。
這是一個簡單的演示,如下所示:
Model:
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
}
public class Category
{
public int Id { get; set; }
public string CName { get; set; }
}
看法:
@model Test
<h1>Edit</h1>
<h4>Test</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div>
<label asp-for="Category"></label>
<select asp-for="Category.Id" asp-items="@ViewBag.Category"></select>
<span asp-validation-for="Category.Id" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Controller:
public class TestsController : Controller
{
private readonly YourDbContext _context;
private readonly List<Category> categories;
public TestsController(YourDbContext context)
{
_context = context;
categories = _context.Category.ToList();
}
// GET: Tests/Edit/5
public async Task<IActionResult> Edit(int? id)
{
var test = await _context.Test.FindAsync(id);
ViewBag.Category = new SelectList(categories, "Id", "CName", test.Category.Id);
if (test == null)
{
return NotFound();
}
return View(test);
}
// POST: Tests/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, Test test)
{
//do your stuff...
//...
//repopulate the selectlist
ViewBag.Category = new SelectList(categories, "Id", "CName", test.Category.Id);
return View(test);
}
}
主頁.cshtml:
@model BookModel
<form method="post" asp-controller="Home" asp-action="Home" role="post">
<div class="form-group">
<label asp-for="bookName"></label>
<select name="bookName" asp-items="@ViewBag.message">
</select>
</div>
<div class="form-group">
<label asp-for="Author"></label>
<input asp-for="Author" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</form>
家庭控制器:
public IActionResult Home()
{
GetBooksDDL();
return View();
}
public void GetBooksDDL(string bookname = "")
{
List<BookModel> bookName = new List<BookModel>();
//for easy testing,I just manually set the value
bookName = new List<BookModel>() {
new BookModel(){ ID=1, bookName="aaa",Author="aaa"},
new BookModel(){ ID=2, bookName="bbb",Author="bbb"},
new BookModel(){ ID=3, bookName="ccc",Author="ccc"}
};
bookName.Insert(0, new BookModel { ID = 0, bookName = "" });
ViewBag.message = new SelectList(bookName, "ID", "bookName", bookname);
}
[HttpPost("[action]")]
[Produces("application/json")]
public async Task<IActionResult> Home(BookModel bookModel)
{
//do all the submit actions
//after all of the actions are complete return the view:
GetBooksDDL(bookModel.bookName);
return View(bookModel);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.