【发布时间】:2020-04-03 19:19:01
【问题描述】:
用 ASP.Net Core MVC + Angular 编写 我正在尝试添加新配方,但我只有从选择选项中获取价值时遇到问题。 当我添加没有类别的新食谱时,一切都很好。 我正在使用响应式表单进行验证。
我的课:
export interface Category {
id: number;
name: string;
recipes?: Recipe[];
}
export interface Recipe {
id: number;
name: string;
ingredients: string;
preparationTime: number;
dateAdded: Date;
photoUrl: string;
description?: string;
recipePhotos?: RecipePhoto[];
}
在我的 add-recipe.component.ts 中
export class AddRecipeComponent implements OnInit {
categories: Category[];
user: User;
recipe: Recipe;
addRecipeForm: FormGroup;
constructor(private fb: FormBuilder,
private alertifyService: AlertifyService,
private router: Router,
private recipeService: RecipeService, private authService: AuthService) { }
ngOnInit() {
this.createRecipeForm();
this.getCategories();
}
createRecipeForm() {
this.addRecipeForm = this.fb.group({
name: ['', Validators.required],
ingredients: ['', Validators.required],
preparationTime: ['', Validators.required],
description: ['', Validators.required],
categoryId: ['', Validators.required]
});
}
createRecipe(id: number) {
if (this.addRecipeForm.valid) {
this.recipe = Object.assign({}, this.addRecipeForm.value);
this.recipeService.addNewRecipe(this.authService.currentUser.id, this.recipe).subscribe(() => {
this.alertifyService.success('New recipe has been added!');
}, error => {
this.alertifyService.error(error);
}, () => {
this.router.navigate(['/recipes']);
});
}
}
cancel() {
this.router.navigate(['members']);
this.alertifyService.warning('Cancelled');
}
getCategories() {
this.recipeService.getCategories().subscribe(response => {
this.categories = response;
}, error => {
this.alertifyService.error(error);
})
}
}
在我的 HTML 中
<div class="container" >
<div class="row justify-content-center">
<div class="col-lg body-card-shadow">
<form [formGroup]="addRecipeForm" class="p-5"
(ngSubmit)="createRecipe()">
<h2 class="text-center text-primary"> ADD NEW RECIPE</h2>
<hr>
<div class="form-group">
<input type="text" [ngClass]="{
'is-invalid':
addRecipeForm.get('name').errors &&
addRecipeForm.get('name').touched
}" class="form-control" formControlName="name" placeholder="Name" />
<div class="invalid-feedback">Please name your Recipe</div>
</div>
<div class="form-group">
<input type="text" [ngClass]="{
'is-invalid':
addRecipeForm.get('ingredients').errors &&
addRecipeForm.get('ingredients').touched
}" class="form-control" formControlName="ingredients" placeholder="Ingredients.." />
<div class="invalid-feedback"
*ngIf="addRecipeForm.get('ingredients').touched && addRecipeForm.get('ingredients').hasError('required')"> Ingredients are required
</div>
</div>
<div class="form-group">
<input type="number" [ngClass]="{
'is-invalid':
addRecipeForm.get('preparationTime').errors &&
addRecipeForm.get('preparationTime').touched
}" class="form-control" formControlName="preparationTime" placeholder="Preparation Time" />
<div class="invalid-feedback"
*ngIf="addRecipeForm.get('preparationTime').touched && addRecipeForm.get('preparationTime').hasError('required')"> Preparation Time is required
</div>
</div>
<div class="form-group">
<textarea cols=100% rows="6" [ngClass]="{
'is-invalid':
addRecipeForm.get('description').errors &&
addRecipeForm.get('description').touched
}" class="form-control" formControlName="description" placeholder="Description"></textarea>
<div class="invalid-feedback"
*ngIf="addRecipeForm.get('description').touched && addRecipeForm.get('description').hasError('required')"> Descripe your recipe, it's important!
</div>
</div>
<div class="form-group">
<label for="category">Category</label>
<select [ngClass]="{
'is-invalid':
addRecipeForm.get('category').errors &&
addRecipeForm.get('category').touched
}" class="form-control" formControlName="category">
<option *ngFor="let category of categories">{{category.name}}</option>
</select>
<div class="invalid-feedback"
*ngIf="addRecipeForm.get('category').touched && addRecipeForm.get('category').hasError('required')"> You must select category
</div>
</div>
<div class="form-group text-center">
<button class="btn btn-success" [disabled]="!addRecipeForm.valid" type="submit">Add</button>
<button class="btn btn-default" type="button" (click)="cancel()">
Cancel
</button>
</div>
</form>
</div>
</div>
</div>
【问题讨论】:
-
什么是表单值?类别选项是否为空?只需登录
this.addRecipeForm.value我也无法在您的 js 文件中看到类别。 ngMolde 正在尝试绑定到那个 -
我添加了更多代码..