ASP.NET 核心 3.1 select 标签 return 空
ASP.NET Core 3.1 select tag return null
我试图在提交时从表单中获取数据并将其保存到 SQL 数据库中,但是当我点击提交时 select 标签出现问题 returns [HttpPost]
操作的空值。
我尝试了网上的所有解决方案,但都没有得到答案...
请帮帮我...
我的代码是:
DbContext
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Employee> Employee { get; set; }
public DbSet<Coding> Coding { get; set; }
}
员工 型号:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string NationalCode { get; set; }
public Coding City { get; set; }
}
编码型号:
public class Coding
{
public int Id { get; set; }
public string City { get; set; }
}
家控制器:
[HttpGet]
public IActionResult Create()
{
var cityList = _dbContext.Coding.ToList();
ViewBag.city = cityList;
return View();
}
[HttpPost]
public IActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
_employeeRepository.AddEmployee(employee);
return View();
}
return View();
}
Create.cshtml 视图:
@model Emp.Models.Employee
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form asp-controller="Home" asp-action="Create" method="post">
<table>
<tr>
<td>
<label asp-for="Name"></label>
</td>
<td>
<input asp-for="Name" />
</td>
</tr>
<tr>
<td>
<label asp-for="NationalCode"></label>
</td>
<td>
<input asp-for="NationalCode" />
</td>
</tr>
<tr>
<td>
<label asp-for="City"></label>
</td>
<td>
<select asp-for="City"
asp-items=@(new SelectList(ViewBag.city , "Id", "City"))
class="custom-select">
</select>
</td>
</tr>
</table>
<button type="submit">submit</button>
</form>
</body>
</html>
请帮忙
你能给我一个我定义的 ViewModel 的例子吗 类?
您应该使用 ViewModel 或添加可能是 CityId
的新 属性 而不是对 City
使用 Coding
class
在select标签中使用asp-for="City.Id"
:
<select asp-for="City.Id"
asp-items=@(new SelectList(ViewBag.city , "Id", "City"))
class="custom-select">
</select>
我试图在提交时从表单中获取数据并将其保存到 SQL 数据库中,但是当我点击提交时 select 标签出现问题 returns [HttpPost]
操作的空值。
我尝试了网上的所有解决方案,但都没有得到答案...
请帮帮我...
我的代码是:
DbContext
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Employee> Employee { get; set; }
public DbSet<Coding> Coding { get; set; }
}
员工 型号:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string NationalCode { get; set; }
public Coding City { get; set; }
}
编码型号:
public class Coding
{
public int Id { get; set; }
public string City { get; set; }
}
家控制器:
[HttpGet]
public IActionResult Create()
{
var cityList = _dbContext.Coding.ToList();
ViewBag.city = cityList;
return View();
}
[HttpPost]
public IActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
_employeeRepository.AddEmployee(employee);
return View();
}
return View();
}
Create.cshtml 视图:
@model Emp.Models.Employee
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form asp-controller="Home" asp-action="Create" method="post">
<table>
<tr>
<td>
<label asp-for="Name"></label>
</td>
<td>
<input asp-for="Name" />
</td>
</tr>
<tr>
<td>
<label asp-for="NationalCode"></label>
</td>
<td>
<input asp-for="NationalCode" />
</td>
</tr>
<tr>
<td>
<label asp-for="City"></label>
</td>
<td>
<select asp-for="City"
asp-items=@(new SelectList(ViewBag.city , "Id", "City"))
class="custom-select">
</select>
</td>
</tr>
</table>
<button type="submit">submit</button>
</form>
</body>
</html>
请帮忙
你能给我一个我定义的 ViewModel 的例子吗 类?
您应该使用 ViewModel 或添加可能是 CityId
的新 属性 而不是对 City
Coding
class
在select标签中使用asp-for="City.Id"
:
<select asp-for="City.Id"
asp-items=@(new SelectList(ViewBag.city , "Id", "City"))
class="custom-select">
</select>