Select MVC .net 核心中的组合框链接到一个 ID
Select combobox in MVC .net core linked to a ID
我试图学习 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”中保存“历史记录”,而不是 ID“1”。
如果这是真的,我如何强制保存 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" />
}
和这本书 class:
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; }
}
将post改成这个。您没有从页面
获得 posted 详细信息
[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);
}
我试图学习 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”中保存“历史记录”,而不是 ID“1”。
如果这是真的,我如何强制保存 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" />
}
和这本书 class:
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; }
}
将post改成这个。您没有从页面
获得 posted 详细信息[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);
}