MVC error when trying to use model in view - c#

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

Related

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies... and requires a model item of type

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.

Cannot implicitly convert linq result into viewmodel list

I'm new to MVC and struggling to implement a ViewModel to query multiple tables. Initially my setup worked perfectly but now having reloaded the project I am getting a compilation error as copied below:
Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>'
ViewModel code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestProject.Models
{
public class COMPCATEGORY
{
public List<COMP> Comp { get; set; }
public List<CATEGORY> Category { get; set; }
}
}
Controller Code:
namespace TestProject.Controllers
{
public class COMPsController : Controller
{
private mattbeaneyEntities1 db = new mattbeaneyEntities1();
// GET: COMPs
public ActionResult Index()
{
COMPCATEGORY viewModel = new COMPCATEGORY();
viewModel.Category = db.CATEGORies.ToList();
viewModel.Comp = db.COMPs.ToList();
return View(viewModel);
}
DB Context code:
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class mattbeaneyEntities1 : DbContext
{
public mattbeaneyEntities1()
: base("name=mattbeaneyEntities1")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<CATEGORY> CATEGORies { get; set; }
public virtual DbSet<COMP> COMPs { get; set; }
}
As the error suggests it is having trouble with two types by the same name, when we were only expecting one. The namespace is our clue here:
Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>'
Usually there are a couple of things I like to check in this case:
Firstly: are there two classes that share the same name and belong to different namespaces? It's quite easy to do as your project grows!
Secondary: has the main project namespace changed? Sometimes due to a bit of refactoring we change the project name and then end up with two .dll files in the bin folder, which hold duplicate of all our classes - delete the old one!

Data annotation issue in EF

HI I have following entity class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace myspace
{
class aList
{
public int Id { get; set; }
[Index("atIdIndex", IsUnique=true)]
public int atId { get; set; }
public string description { get; set; }
}
}
In visual studio on [Index("atIdIndex", IsUnique=true)] I get red line under Index and it says that
Type or namespace Index couldnot be found.
Please let me know how i can fix it Thanks
'
You need to add:
using System.ComponentModel.DataAnnotations.Schema;
Had to re install EF it worked fine after that. Thanks

TFS allowing me to access just one class out of five classes in the same folder in .net

I'm very new to working with TFS(of course not with Visual Studio). I have 6 projects in same solution and one of the projects has a folder with 6 c-sharp classes in it. When i try to access these classes from inside of other projects ( which all are in the same solution) except one class other five classes don't appear in the intellisense.
I have manually added reference to this project in other project. Which is why I'm able to access one class but my confusion is why not other classes are accessible through intellisense.
Did I do something wrong. I did check-in whatever pending changes were there in the entire solution still no luck.
Your help will be highly appreciated
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NS.GIS.PTC.Core.Entities;
namespace NS.GIS.PTC.WebServices.DataManagement.Model.Dtos
{
public class FieldDescriptionDto
{
public string Name
{
get;
set;
}
public string Type
{
get;
set;
}
public string Alias
{
get;
set;
}
public int Length
{
get;
set;
}
public bool IsSystem
{
get;
set;
}
public Domain Domain
{
get;
set;
}
}
}
here is the code and below is the code that is using it I have written it forcibly coz as u know its not appearing in intellisense
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NS.GIS.PTC.Core.Entities;
using NS.GIS.PTC.WebService.DataManagement.Model.Dtos;
namespace NS.GIS.PTC.WebService.DataManagement.API.Model
{
internal static class FieldDescriptionExtension
{
internal static FieldDescriptionDto ToFieldDescriptionDto(this FieldDescription fieldDescription)
{
return new FieldDescriptionDto
{
Name = fieldDescription.Name,
Type = fieldDescription.Type,
Alias= fieldDescription.Alias,
};
}
}
}
Please check if all the classes are marked Public.
Check if their namespaces have been included.
Restart VS and try.
Forcibly use them and see if you get a squiggle to auto-include namespace etc.

MVC 5 ViewModel not working as it was in MVC 4

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.

Categories