I get an error on line 14 of the below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeDb.Models;
namespace TimeDb.Areas.Stock.Models
{
public class ProductAddViewModel
{
private readonly DatabaseDataContext _db = new DatabaseDataContext();
public SelectList ProductTypes;
public Product Product { get; set; }
public ProductAddViewModel()
{
Product = new Product();
PopulateLists();
}
public ProductAddViewModel(int id)
{
Product = _db.Products.Single(p => p.Id == id);
PopulateLists();
}
private void PopulateLists()
{
ProductTypes = new SelectList(_db.ProductTypes.OrderBy(pt => pt.Name), "Id", "Name");
}
}
}
Now, this has only appeared since I tried adding a new field to Database.cs.
What I was trying to do was to add a new field, I had added within a database table. But the error appeared above.
Part of the database code I tried changing was:
#region Product
[MetadataType(typeof(ProductMetaData))]
partial class Product
{
}
public class ProductMetaData
{
[Required]
[DisplayName("Name")]
public string Name { get; set; }
[Required]
[DisplayName("Description")]
public string Description { get; set; }
[Required]
[DisplayName("Retail Price")]
public double RetailPrice { get; set; }
[Required]
[DisplayName("Threshold")]
public int Threshold { get; set; }
[Required]
[DisplayName("Number To Keep In Stock")]
public int NumberToKeepInStock { get; set; }
[Required]
[DisplayName("Shelf")]
public string Shelf { get; set; }
}
I added:
[DisplayName("disabled")]
public int disabled { get; set;}
Most likely I am not aware of the best way of adjusted my database code once I adjust the table. But when I remove the above amendment, it still shows the same error.
What’s the best way forward?
Thanks
Your
Productclass isn’tpublic, which means it will be treated asinternal. When you expose a property of typeProductaspublicof a class that is itselfpublicthe property will be visible outside the current assembly. However, theProductclass isinternaland not visible. That’s the inconsitency.Either make your
Productclasspublicor yourProductpropertyinternal.As this is an MVC view model you probably want to use the
publicoption to have the property visible to the view (the view is compiled in a separate assembly and cannot accessinternalproperties of the view model).