Reputation: 427
hello community I have a problem I have two classes product and price are related, so I am creating a form in which I need to put in the bind-value a property that comes from both classes, but when I run the application it does not show me the form.
the problem is in this line:
<InputNumber class="form-control" @bind-Value="@Producto.Precio.PrecioSinImpuesto"></InputNumber>
i need property PrecioSinImpuesto
but it is in the price class and not in the product class
How can I make that bind work?
this is my two class:
public class Producto
{
[Key]
public Guid ProductoId { get; set; }
public Guid InquilinoId { get; set; }
public string Nombre { get; set; }
public decimal Precio_Publico { get; set; }
public string Detalle_producto { get; set; }
public DateTime? Fecha_publicacion { get; set; }
public bool Activo { get; set; }
public string Img_Producto { get; set; }
public string ClaveProducto { get; set; }
public string CodigoBarras { get; set; }
public Concepto VigenciaPrecio { get; set; }
public virtual ICollection<Precio> Precios { get; set; }
public Guid ImpuestoId { get; set; }
public Guid PrecioId { get; set; }
public List<CategoriaProducto> CategoriaProducto { get; set; } = new List<CategoriaProducto>();
public List<ProductoPersona> ProductoPersona { get; set; }
public virtual Impuesto Impuesto { get; set; }
public virtual Precio Precio { get; set; }
public string NombreCortado {
get
{
if (string.IsNullOrWhiteSpace(Nombre))
{
return null;
}
if (Nombre.Length > 60)
{
return Nombre.Substring(0, 60) + "...";
}
else
{
return Nombre;
}
}
}
}
public class Precio
{
[Key]
public Guid PrecioId { get; set; }
public DateTime? Fecha_Inicio {get; set;}
public DateTime? Fecha_Default { get; set; }//ver si la podemos eliminar
public DateTime? Fecha_Final { get; set; }
public decimal PrecioSinImpuesto { get; set; }
public Concepto ListadoPrecios { get; set; }//mayoreo menudeo
}
console error when entering the product form:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at BlazorApp.Client.Pages.Producto.FormularioProducto.<BuildRenderTree>b__0_1 (Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder2) [0x002de] in <filename unknown>:0
at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent (System.Int32 sequence, Microsoft.AspNetCore.Components.RenderFragment fragment) <0x316ab58 + 0x0001e> in <filename unknown>:0
at Microsoft.AspNetCore.Components.CascadingValue`1[TValue].Render (Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder) <0x368aa18 + 0x00012> in <filename unknown>:0
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch (Microsoft.AspNetCore.Components.Rendering.RenderBatchBuilder batchBuilder, Microsoft.AspNetCore.Components.RenderFragment renderFragment) <0x30c0dd8 + 0x00062> in <filename unknown>:0
at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch (Microsoft.AspNetCore.Components.Rendering.RenderQueueEntry renderQueueEntry) <0x30c0798 + 0x0004c> in <filename unknown>:0
at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue () <0x30bfcd0 + 0x00098> in <filename unknown>:0
Upvotes: 0
Views: 205
Reputation: 273244
When
<InputNumber class="form-control" @bind-Value="@Producto.Precio.PrecioSinImpuesto"></InputNumber>
gives you a null-reference exception then either .Precio
or .Precio.PrecioSinImpuesto
is null
.
This is caused by how you load the data, not by Blazor. You probably need to .Include()
things in your query. But you didn't post that part, no precise answer is possible.
Upvotes: 4