I have Profile table but profiles is in red and gives me an error that 'LoginDataContext' does not contain a definition for 'profiles'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace practice
{
public partial class LoginDataContext
{
public Profile get_profileid(int id)
{
return this.profiles.SingleOrDefault(p => p.profile_id == id);
}
}
}
I'm assuming I'm missing a namespace declared on the top of my controller but I tried with 'using WebMatrix.Data;' and 'using System.IO;' that they are the usual suggestions for this error. It didn't work.
I'm trying to display more than one database table in the same view and this error is stopping me. Help please.
My Controller:
using System.Linq;
using System.Web.Mvc;
using KMS.Models;
using WebMatrix.Data;
using System.IO;
namespace KMS.Controllers
{
public class KMSController : Controller{
public ActionResult Index()
{
KMSConection cs = new KMSConection();
cs.Areas = (from o in db.Areas select o).Tolist();
cs.AreaTypes = (from o in db.AreaTypes select or).Tolist();
return View(cs);
}
}
}
My ViewModel Class:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using KMS.Models;
namespace KMS.Models
{
public class KMSConection: ApplicationDbContext
{
public DbSet<Area> Areas { get; set; }
public DbSet<AreaType> AreaTypes { get; set; }
}
}
Thanks!
Because you are not mentioning the db variable you have created into your KMSController. Try this code to fix the error:
using System.Linq;
using System.Web.Mvc;
using KMS.Models;
using WebMatrix.Data;
using System.IO;
namespace KMS.Controllers
{
public class KMSController : Controller{
public ActionResult Index()
{
//Insert the actual entity referred to your db variable.
//Probably something like:
//var db = Some Database Context
KMSConection cs = new KMSConection();
cs.Areas = (from o in db.Areas select o).Tolist();
cs.AreaTypes = (from o in db.AreaTypes select or).Tolist();
return View(cs);
}
}
}
Got this error when I launch my 'Details' inside my Index on 'Firmas' (Generic auto-generated MS code):
Error
This is my model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using actiTest2.Context;
using System.Data.Entity;
namespace actiTest2.Models
{
public class PcontacFirmasModel
{
public Pcontac pcontac { get; set; }
public Firmas firmas { get; set; }
}
}
An the only call that I try to do atm:
#model actiTest2.Models.PcontacFirmasModel
#{
ViewBag.Title = Html.DisplayFor(model => model.firmas.NOMCOMP);
}
It seems that in the controller side, you are passing an object of type "Firmas" but your view is expecting the object "PcontacFirmasModel".
Update your action to send the PcontacFirmasModel object. you can use a mapper utility to map PcontacFirmasModel and Firmas.
My problem occurs when I try to tell the view which model to use. It only happens with this model though.. Here is the model code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Com.Domain.Entities
{
public class Components
{
}
public class headerModel
{
public virtual String title { get; set; }
public virtual String subtitle { get; set; }
}
}
In the view when I try to add it like this
#model Com.Domain.Entities.headerModel;
It gives me this error:
only assignment call increment decrement and new object expressions can be used as a statement
I'm not sure what's going on. Any help would be appreciated! Thanks!
Remove ; at the end of the model declaration in your view
#model Com.Domain.Entities.headerModel
I will try to get straight to the point. I had some help building a ViewModel here on StackOverflow. It worked fine in MVC 4 but now that I am converting the application to MVC 5 it is not working. Nothing has changed in way of the code. I have a _navigation.cshtml which is a partial that is rendered in my Layout.cshtml and the error is within the For Loops in that Partial. This same code is working fine in MVC 4. Here is the code:
My error is in the partial page during the for loop and I get the error on Ingredient in the line:
#foreach (Ingredient ingredient in Model.Ingredients)
also in the same place on any other for loop in the same place. The error says:
The type or namespace name 'Recipe' could not be found (are you
missing a using directive or an assembly reference?)
Here is my code:
Models/Ingredient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace XXX.Models
{
public class Ingredient
{
public int IngredientID { get; set; }
public string IngredientNameEs { get; set; }
}
}
Models/Recipe.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace XXX.Models
{
public class Recipe
{
public int RecipeID { get; set; }
public string RecipeNameEs { get; set; }
}
}
Models/IdentityModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Identity.EntityFramework;
using XXX.Models;
namespace XXX.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class,
// http://go.microsoft.com/fwlink/?LinkID=317594 for more.
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(): base("DefaultConnection")
{
}
public DbSet<Ingredient> Ingredients { get; set; }
public DbSet<Recipe> Recipes { get; set; }
}
}
ViewModels/NavigationViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXX.Models;
namespace XXX.ViewModels
{
public class NavigationViewModel
{
public IEnumerable<Ingredient> Ingredients { get; set; }
public IEnumerable<Recipe> Recipes { get; set; }
}
}
Controllers/PartialsController.cs
using XXX.Models;
using XXX.ViewModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace XXX.Controllers
{
public class PartialController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Navigation()
{
NavigationViewModel viewModel;
viewModel = new NavigationViewModel();
viewModel.Ingredients = db.Ingredients.Where(i => i.IsProduct != false).ToList();
viewModel.Recipes = db.Recipes.ToList();
return PartialView("_Navigation", viewModel);
}
}
}
Partials/_Navigation.cshtml (Asterisks indicate error near Ingredient and Recipe in For Loop)
#model XXX.ViewModels.NavigationViewModel
#using XXX.Models
//edited for brevity..
<li class="has-dropdown">#Html.ActionLink(XXX.Views.Shared.CultureSwap.Products, "Products", "Ingredient")
<ul class="dropdown">
#*//From NavigationViewModel print out each Product*#
#foreach (*Ingredient* ingredient in Model.Ingredients)
{
<li>#ingredient.IngredientNameEs</li>
}
</ul>
</li>
<li class="divider"></li>
<li class="has-dropdown">#Html.ActionLink(XXX.Views.Shared.CultureSwap.Recipes, "List", "Recipe")
<ul class="dropdown">
#foreach (*Recipe* recipe in Model.Recipes)
{
<li>#recipe.RecipeNameEs</li>
}
</ul>
</li>
Again the errors read:
The type or namespace name 'Ingredient' could not be found (are you
missing a using directive or an assembly reference?)
The type or namespace name 'Recipe' could not be found (are you
missing a using directive or an assembly reference?)
Here is a screenshot of the error in Visual Studio Next to the same code with no errors:
There is a web.config file located in the Views directory. In it the namespaces that should be available for the views are listed. Did you add a namespace to the views web.config in your mvc4 proj that you are now missing in the mvc5 proj?
The listing in the views web.config is a kind of global using statements that applies to all views.
Your view doesn't know where Ingredient or Recipe come from, you need to add a reference to the namespace which those types are under, add #using XXX.Models to the top of your view
#model XXX.ViewModels.NavigationViewModel
#using XXX.Models
...
#foreach (Ingredient ingredient in Model.Ingredients)
{
...
}
On a side-note you appear to have a half-baked view model implementation. In your NavigationViewModel you are referencing, which appear to be, domain models. It's generally recommended that anything exposed via a view model, is in actual fact, a view model itself. So in your case, I would introduce a couple of new view models to represent an Ingredient / Recipe i.e.
public class IngredientViewModel
{
...
}
public class RecipeViewModel
{
...
}
public class NavigationViewModel
{
public IEnumerable<IngredientViewModel> Ingredients { get; set; }
public IEnumerable<RecipeViewModel> Recipes { get; set; }
}
These would be be created under the XXX.ViewModels which would mean your view would look like
#using XXX.ViewModels
#model NavigationViewModel
...
Your view doesn't know where Ingredient or Recipe come from, you need to add a reference to the namespace which those types are under, add #using XXX.Models to the top of your view.
If your MVC application has Areas, check whether the namespace of the ViewModel and model reference in the Razor view is matched or not.
If the namespace of the view model contains Areas e.g. Areas.WebApp.Models , that view model won't be compiled in the view when you write #model WebApp.Models.
Both should match. So either remove Areas from ViewModel namespace or add the same fully qualified name in the View.