@page "/Data/Article"
@attribute [Authorize(Roles = "admin")]
@using FerrDataAccess.Models
@using FerrDataAccess.Data
@using BlazorServer.Models
@using Microsoft.AspNetCore.Mvc.Rendering;
@inject IArticleData _db
@inject ICategoryData _ca
<h1>Pagina de Articulos</h1>
<h4>Insertar Articulos</h4>
<EditForm Model="@newArticle" OnValidSubmit="@InsertArticle">
<DataAnnotationsValidator />
<ValidationSummary />
<head>
<style type="text/css">
label {
width: 100px;
display: inline-block;
}
</style>
</head>
<label>Title</label> <InputText id="title" @bind-Value="newArticle.Title" />
<p></p>
//Aqui quiero seleccionar mis categorias que tengo en mi base de datos*/
<label>Category</label><InputSelect @bind-Value="newArticle.CategoryId"></InputSelect>
<p> </p>
<label>Price</label> <InputNumber @bind-Value="newArticle.Price" />
w
<button type="submit" class="btn btn-primary">Agregar</button>
</EditForm>
<h4>Articulos</h4>
@if (article is null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table table-striped">
<thead>
<tr>
<th>Titulo</th>
<th>Categoria</th>
<th>Precio</th>
</tr>
</thead>
<tbody>
@foreach (var arti in article)
{
<tr>
<td>@arti.Title</td>
<td>@arti.CategoryId</td>
<td>@arti.Price</td>
</tr>
}
</tbody>
</table>
}
@code {
private List<ArticleModels> article;
private List<CategoryModels> category;
private DisplayArticleModels newArticle = new DisplayArticleModels();
protected override async Task OnInitializedAsync()
{
article = await _db.GetArticle();
}
private async Task InsertArticle()
{
ArticleModels a = new ArticleModels
{
Title = newArticle.Title,
CategoryId = newArticle.CategoryId,
Price = newArticle.Price
};
await _db.InsertArticle(a);
article.Add(a);
newArticle = new DisplayArticleModels();
}
//Este de aqui es el que intente hacer para meter en una lista solo los items nombre
//pero tomado como referencia por el id de cada categoria pero nose como ponerlo dentro del inputselect
private async Task GetCategoria()
{
// CategoryModels c =new CategoryModels
var cate = await _ca.GetCategory();
catelist = new List<SelectListItem>();
cate.ForEach(
x =>
{
catelist.Add(
new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name
});
});
}
public List<SelectListItem> catelist { get; set; }
}