enter code here
我已经成功完成了ID,名称,出生日期,性别的Student表的CRUD,但是当我尝试使用名为StudentCourse的子表进行输入时,该表有多个StudentCourse项,这是行不通的。
我为StudentCourse添加了一个Multiselect Dropdown,但它是从api检索数据但未绑定
任何人都有一个想法如何使子表成为可能。 这是我的代码
@page "/studentpost"
@using BlazorApp.Shared.Models
@using BlazorApp.Shared.Enums
@inject NavigationManager NavigationManager
@inject HttpClient Http
<h3>Add Student Details</h3>
<div>
<EditForm Model="@student">
<DataAnnotationsValidator />
<ValidationSummary />
<table class="form-group">
<tr>
<td>
<label for="Name" class="control-label">Student Name</label>
</td>
<td>
<InputText class="form-control" @bind-Value="@student.Name" />
<ValidationMessage For="@(() => student.Name)" />
</td>
</tr>
<tr class="pt-5">
<td>
<label for="Email" class="control-label">BirthDate</label>
</td>
<td>
<InputDate class="form-control" @bind-Value="@student.BirthDate" />
<ValidationMessage For="@(() => student.BirthDate)" />
</td>
</tr>
<tr class="pt-5">
<td>
<label for="Name" class="control-label">Gender</label>
</td>
<td>
<InputSelect id="DdlGender" @bind-Value="student.Gender">
<option value="0">-- Select Gender --</option>
<option value="@Gender.Type.Male">@Gender.Type.Male.ToString()</option>
<option value="@Gender.Type.Female">@Gender.Type.Female.ToString()</option>
<option value="@Gender.Type.TransGender">@Gender.Type.TransGender.ToString()</option>
</InputSelect>
<ValidationMessage For="@(() => student.Gender)" />
</td>
</tr>
<tr class="pt-5">
<td>
<label for="Name" class="control-label">Course</label>
</td>
<td>
<InputSelect id="DdlCourse" @bind-Value="courses">
<option value="0">-- Select Gender --</option>
@foreach (var itm in courses)
{
<option value="@itm.Id">@itm.CourseName.ToString()</option>
}
</InputSelect>
</td>
</tr>
<tr class="pt-5">
<td>
<button type="submit" class="btn btn-success" @onclick="@(async ()=> await AddStudent())">Save</button>
</td>
<td>
<button type="submit" class="btn btn-success" @onclick="Cancel">Cancel</button>
</td>
</tr>
</table>
</EditForm>
</div>
@code {
Student student = null;
Course[] courses=null;
//protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
student = new Student() {Gender=Gender.Type.Female ,BirthDate = DateTime.Now };
courses=await Http.GetJsonAsync<Course[]>("course");
}
public async Task<Course[]> GetCourses()
{
return await Http.GetJsonAsync<Course[]>("course");
}
void BackToList()
{
NavigationManager.NavigateTo($"/fetchstudent");
}
void Cancel()
{
BackToList();
}
protected async Task AddStudent()
{
await Http.SendJsonAsync(HttpMethod.Post, "student/add", student);
BackToList();
}
//private void GenderChanged(UIChangeEventArgs e)
//{
//}
}
在共享项目中,我从数据库获得模型
public class Student
{
public int Id { get; set; }
[Required ]
[StringLength(150, ErrorMessage ="Name must be within 150 characters.")]
public string Name { get; set; }
[Required]
public DateTime BirthDate { get; set; }
[Required]
[Range(1,3)]
public Gender.Type Gender { get; set; }
// public List<int> CourseIds { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CourseName { get; set; }
}
在上下文中,我已经注册了该模型
public class CoreDemoContext : DbContext
{
//public CoreDemoContext(DbContextOptions<CoreDemoContext> options) : base(options)
//{
//}
public CoreDemoContext()
{
}
public DbSet<Student> Student { get; set; }
public DbSet<Course> Course { get; set; }
public DbSet<StudentCourse> StudentCourse { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Data Source=RITA-PC\SQLEXPRESS;Initial Catalog=CoreDemo;Persist Security Info=True;User ID=sa;Password=sa@123");
}
}
}
答案 0 :(得分:0)
@bind-Value
应该是所选项目,而不是数组:
<InputSelect id="DdlCourse" @bind-Value="selectedCourse">
和@code
Course selectedCourse = null;