c# - 在链接到 ID 的 MVC .net 核心中选择组合框
问题描述
我试图学习 MVC .net 核心,我需要填充一个组合框并将组合框的 ID 保存到数据库中。
我的控制器中有这段代码:
ViewBag.Categories = new List<SelectListItem>
{
new SelectListItem{ Text="history", Value="1"},
new SelectListItem{ Text="literature", Value="2"},
};
这是我认为的代码:
<div class="form-group row">
<div class="col-3">
<label asp-for="IdCategory">Category</label>
</div>
<div class="col-6">
<select asp-for="IdCategory" name="CategoryId" asp-items="ViewBag.Categories">
<option>-- select the category --</option>
</select>
<span asp-validation-for="IdCategory" class="text-danger"></span>
</div>
</div>
组合框正确显示了内容,但在我将字段“IdCategory”保存到数据库后 = 0。
我的印象是视图试图在字段“IdCategory”中保存“历史”而不是“1”,即 ID。
如果这是真的,我怎么能强制保存 ID 而不是文本?
谢谢
在此处更新控制器:
public IActionResult Upsert(int? id)
{
ViewBag.Categories = new List<SelectListItem>
{
new SelectListItem{ Text="history", Value="1"},
new SelectListItem{ Text="literature", Value="2"},
};
Book = new Book();
if (id == null)
{
//create
return View(Book);
}
//update
Book = _db.Books.FirstOrDefault(u => u.Id == id);
if (Book == null)
{
return NotFound();
}
return View(Book);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Upsert()
{
if (ModelState.IsValid)
{
if (Book.Id == 0)
{
//create
_db.Books.Add(Book);
}
else
{
_db.Books.Update(Book);
}
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(Book);
}
这里是完整的视图:
@model BookListMVC.Models.Book
<br />
<h2 class="text-info">@(Model.Id!=0 ? "Edit" : "Create") Book</h2>
<br />
<div class="border container" style="padding:30px;">
<form method="post">
@if (Model.Id != 0)
{
<input type="hidden" asp-for="Id" />}
<div class="text-danger" asp-validation-summary="ModelOnly"></div>
<div class="form-group row">
<div class="col-3">
<label asp-for="Name"></label>
</div>
<div class="col-6">
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="Author"></label>
</div>
<div class="col-6">
<input asp-for="Author" class="form-control" />
<span asp-validation-for="Author" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="ISBN"></label>
</div>
<div class="col-6">
<input asp-for="ISBN" class="form-control" />
<span asp-validation-for="ISBN" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="ISBN"></label>
</div>
<div class="col-6">
<input asp-for="ISBN" class="form-control" />
<span asp-validation-for="ISBN" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="IdCategory">Category</label>
</div>
<div class="col-6">
<select asp-for="IdCategory" name="CategoryId" asp-items="@ViewBag.Categories">
<option>-- select the category --</option>
</select>
<span asp-validation-for="IdCategory" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3 offset-3">
<button type="submit" class="btn btn-primary form-control">
@(Model.Id != 0 ? "Update" : "Create")
</button>
</div>
<div class="col-3">
<a asp-action="Index" class="btn btn-success form-control">Back to List</a>
</div>
</div>
</form>
</div>
@section Scripts{
<partial name="_ValidationScriptsPartial" />
}
和书类:
public class Book
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
public int IdCategory { get; set; }
}
解决方案
把帖子改成这个。您没有从页面获取发布的详细信息
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Upsert(BookListMVC.Models.Book model)
{
if (ModelState.IsValid)
{
if (model.Id == 0)
{
//create
_db.Books.Add(model);
}
else
{
_db.Books.Update(model);
}
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
推荐阅读
- android - Linphone Android Build Compile Error: ChatMessagesFragment is not abstract
- python - How can I automate a python code for many cells?
- google-drive-api - Google Drive API V3 - 复制功能不复制内容
- java - 如何从循环外部访问 for 循环中的值?
- sql - 来自其他表的分组日期范围
- python - 取消 Build 密钥在 Sublime Text 3 中不起作用
- asp.net-core - 检测到可能的对象循环。在 System.Text.Json 和 Newtonsoft.Json
- windows-10 - 右键单击所选文本打开 URL
- java - 如何使用自定义 jooq 生成器并将其用作 sbt 任务?
- c++ - 网格相交 3D