Visual Studio 2013 not recognizing reference to my new Model? - c#

I'm working on an ASP.Net application. For one part of this, I have particular models stored in their own folder:
I have a reference to my Models/Default/ folder in one of my CustomControls: using Project.Models.Default; However, when I try to make a reference in my CustomControl YearsOfService to my default Model of Years_Of_Service, Visual Studio (2013) is not picking up either my Years_Of_Service or Years_Of_Service_Data model with my Models/Default/ folder:
Anyone have any ideas why this is happening?
EDIT:
Years_Of_Service.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PROJECT.Models
{
public class YearsOfService
{
public string Year { get; set; }
public decimal ServiceCredited { get; set; }
public decimal Salary { get; set; }
public string CoveredEmployer { get; set; }
}
}
Years_Of_Service_Data.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PROJECT.Models
{
public class YearsOfServiceData
{
public List<YearsOfService> YearsOfService { get; set; }
}
}

You currently have:
namespace PROJECT.Models
But this should also contain Default like this:
namespace PROJECT.Models.Default
So, it should end up like this:
namespace PROJECT.Models.Default
{
public class YearsOfServiceData
{
public List<YearsOfService> YearsOfService { get; set; }
}
}
Finally, you may want to keep your file names and class names the same otherwise it can get very confusing! So, stick with either Years_Of_Service_Data or YearsOfServiceData in both file and class name.

The classes contained in those files are just in the wrong namespace. Specify the correct one like this:
namespace XXXX.Models.Default
{
public class YearsOfService
{
//Snip
}
}
Alternatively, refer to them with their namespace as XXXX.Models.YearsOfService

Related

The type name 'Ceiling' does not exist in the type 'Math'

I am working on a home project on developer edition of VS2015 and I am getting an error saying that "The type name 'Ceiling' does not exist in the type 'Math'". Although I can see this property in the Math class, but it doesn't let me compile. I am not sure what I am missing here. I also tried to search on internet , but no help..
Thanks!!
Please find the code below: it's simple pagination.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FoodStore.WebUI.Models
{
public class PagingInfo
{
public int TotalItems { get; set; }
public int CurrentPage { get; set; }
public int ItemsPerPage { get; set; }
public int TotalPages
{
get { return (int)(Math.Ceiling)((decimal)TotalItems/ItemsPerPage ); }
}
}
}
You have a syntax error:
get { return (int)(Math.Ceiling((decimal)TotalItems / ItemsPerPage)); }

Error 1 Error 2 on Conference MVC Tutorial

Hi there im following the series of MVC tutorials given by microsoft and i came across this problem:
Error 1 The best overloaded method match for
'System.Data.Entity.DbSet.Add(Conference.Models.Session)'
has some invalid arguments visual studio
2013\Projects\Conference\Conference\Models\ConferenceContextInitializer.cs 18 31 Conference
Error 2 Argument 1: cannot convert from 'Conference.Models.Speaker' to
'Conference.Models.Session' visual studio
2013\Projects\Conference\Conference\Models\ConferenceContextInitializer.cs 19 34 Conference
ConferenceModelInitializer
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Conference.Models
{
public class ConferenceContextInitializer : DropCreateDatabaseAlways<ConferenceContext>
{
protected override void Seed(ConferenceContext context)
{
context.Sessions.Add(
new Session()
{
Title = "I Want Spaghetti",
Abstract = "The Life and times of a spaghetti lover",
Speaker = context.Speakers.Add(
new Speaker()
{
Name = "Jon Pepe",
EmailAddress = "jon#asfdasd.com"
})
});
context.SaveChanges();
}
}
}
ConferenceContext
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Conference.Models
{
public class ConferenceContext : DbContext
{
public DbSet<Session> Sessions { get; set; }
public DbSet<Session> Speakers { get; set; }
}
}
Thank you!
replace:
public class ConferenceContext : DbContext
{
public DbSet<Session> Sessions { get; set; }
public DbSet<Session> Speakers { get; set; }
}
with:
public class ConferenceContext : DbContext
{
public DbSet<Session> Sessions { get; set; }
public DbSet<Speaker> Speakers { get; set; } //since you are referencing the speaker class here
}

Add View - Cannot retrieve Metadata

I am still new to MVC, but I am starting to get a feel for the general picture however I still mixup things like namespace, or using and I THINK that may be the case here where I am mis-referencing something.
Question: I am trying to add an EmployeeInfo View, with a List template, with Model class: EmployeeInfo, with data context class: MoviesEntities
The auto-generation will not execute. When I right-click in Controller's method EmployeeInfo. I select the option "Add View", fill in the information, then hit add and during the scaffolding load screen it gives me the error as follows.
There was an error running the selected code generator: 'Unable to
retrieve metadata for 'WebApplication2.Models.EmployeeInfo'.'
My controller
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Entities;
using WebApplication2.Models;
namespace WebApplication2.Controllers
{
public class MoviesController : Controller
{
private MoviesEntities db = new MoviesEntities();
// GET: /Movies/
public ActionResult Index()
{
return View(db.Movies.ToList());
}
public ActionResult EmployeeInfo()
{
var query =
from m in db.Movies
join me in db.MovieEmployees
on m.ID equals me.movieID
join e in db.Employees
on me.employeeID equals e.ID
join r in db.Roles
on me.roleID equals r.ID
select new EmployeeInfo() {Name = e.Name, Role = r.RoleType, Birthday = e.Birthday };
return View(query.Distinct().ToList());
}
}
}
My context
//------------------------------------------------------------------------------
// <auto-generated> Built from database Movie </auto-generated>
//------------------------------------------------------------------------------
namespace WebApplication2.Entities
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using movieLayer;
using WebApplication2.Models;
public partial class MoviesEntities : DbContext
{
public MoviesEntities()
: base("name=MoviesEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<Location> Locations { get; set; }
public virtual DbSet<Movie> Movies { get; set; }
public virtual DbSet<MovieEmployee> MovieEmployees { get; set; }
public virtual DbSet<Role> Roles { get; set; }
public virtual DbSet<Show> Shows { get; set; }
public virtual DbSet<sysdiagram> sysdiagrams { get; set; }
public System.Data.Entity.DbSet<WebApplication2.Models.EmployeeInfo> EmployeeInfoes { get; set; }
}
}
Model of EmployeeInfo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication2.Models
{
public class EmployeeInfo
{
public EmployeeInfo() { }
public string Name { get; set; }
public string Role { get; set; }
public DateTime Birthday { get; set; }
}
}
What I did to solve this issue that I encountered when adding a scaffolded view by right clicking the controller action was to leave the "Data context class" field blank.
I'm not entirely sure as to why this works but I found this method from ASP.NET forums.
Make sure you rebuild your solution first. If you just added a new class, the scaffolding engine may not actually know about it, yet, if the project hasn't compiled.
If that doesn't work, close Visual Studio and restart. I have seen the scaffolding stuff just go wonky for no good reason in VS2012, and a restart will usually fix it.
The problem was the EmployeeInformation was just a "template" for the controller to dump it's data into from the query for the View.
EmployeeInformation was not actually an existing table, thus I had to make a model i.e. EmpleeInfoModels to hold the data, that was then passed to my custom View.
Method
public ActionResult EmployeeInformation()
{
var query =
from m in db.Movies
join me in db.MovieEmployees
on m.ID equals me.movieID
join e in db.Employees
on me.employeeID equals e.ID
join r in db.Roles
on me.roleID equals r.ID
select new EmployeeInfoModels() { Name = e.Name, RoleType = r.RoleType, Birthdate = e.Birthday, eID = e.ID };
return View(query.Distinct().ToList().Distinct());
}
Model
namespace WebApplication2.Models
{
public class EmployeeInfoModels
{
public int mID { get; set; }
public int eID { get; set; }
public string Name { get; set; }
public string RoleType { get; set; }
public DateTime Birthdate { get; set; }
}
}
And my View just brought in
#model IEnumerable<WebApplication2.Models.EmployeeInfoModels>
Adding one more tip: you may have this error if you have any configSource attributes in any of the elements in your web.config. I commented out all of the elements that contained a configSource attribute and then ran the scaffolding again and it worked like a charm. Once it was finished, I uncommented the elements in the web.config and I was all set.
Check your DataContextClass Option in Add View Pop winow and then correct the ConnectionString when adding your View from Controller
Refer Screenshot
.

add item into List<string> from another class in c#

I have 3 class. Those are places.cs, onePlace.cs and placestovisit.cs.
placestovisit.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sunamganj.ViewModels
{
public class placestovisit
{
public bool IsDataLoaded { get; set; }
public onePlace sunamganj { get; set; }
public static string basePlaces = "Assets/Places/";
private string baseTanguar = basePlaces + "Tanguar/";
private string baseBaruni = basePlaces + "Baruni/";
public void LoadData()
{
sunamganj = createSunamganj();
IsDataLoaded = true;
}
private onePlace createSunamganj()
{
onePlace data = new onePlace();
data.Items.Add(new places()
{
ID = "0",
Title = "Tanguar Haor",
shortDescription="Tanguar Haor (Lowlaying marsh) is a complex landscape of over 46 marshes, 30 km Northwest of Sunamgonj District.",
itemImage = baseTanguar + "1.jpg",
FullDescription = "Tanguar Haor (Lowlaying marsh) is a complex landscape of over 46 marshes, 30 km Northwest of Sunamgonj District. The marshes are inter connected with one another through narrow Channels but merge into a single large water body during monsoon. The aquatic vegetation and less disturbance from the human are instrument to invite a large variety of waterfowl specially winter migrant ducks that congregates in thousands. Resident and local migrant, raptor, waders and passerine birds made the area as one of the Asia's most potential birding place. Tanguar Haor is listed as a Ramsar site under the Ramsar Convention in 2000."
});
}
}
onePlace.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sunamganj.ViewModels
{
public class onePlace
{
public string Title { get; set; }
public List<places> Items { get; set; }
public onePlace()
{
Items = new List<places>();
}
}
}
places.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sunamganj.ViewModels
{
public class places
{
public string ID { get; set; }
public string Title { get; set; }
public string shortDescription { get; set; }
public string FullDescription { get; set; }
public string itemImage { get; set; }
public List<string>Gallery { get; set; }
}
}
I want to add item into Gallery from placestovisit class. For that what to do?
Actually I want to add one photo gallery for each object. But I am not so much good in OOP. At this moment can I go with this concept or need to change the concept. If I can go with this one then how can I add item into Gallery from placestovisit class?
Gallery is just a property of your places class, and you can add items by accessing that property via an instance of places class.
One thing you should remember that the properties of reference types are null by default, so you need to initialize them.You can do that in your constructor:
public class places
{
public places()
{
Gallery = new List<string>();
}
}
Then you can add new items to your list like:
var plc = new places() { /* set the properties */ }
plc.Gallery.Add("new gallery");

How to load data from database in a ListBox?

I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.
I want to load data from my database in a ListBox, I did many searches but I don't find anything where Entity Framework is used.
In fact, I tried many solutions but always VS didn't accept the syntax.
This my model Class :
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data.Entity;
namespace MvcApplication2.Models
{
public class Profile_Ga
{
[Key]
public string ID_Gamme { get; set; }
public string In_Ga { get; set; }
public string Out_Ga { get; set; }
public string Next_Gamme { get; set; }
public string Etat { get; set; }
}
}
I want to display ID_Gamme in a listBox?
In the view I did like this by it is not accepted.
<%: Html.ListBoxFor(model => model.ID_Gamme) %>
There is any solution?
ID_Gamme is a string (a singular value), so you can't make a list box for a singular value.
My guess is that you need a List<Profile_Ga> (or IEnumerable) to do the binding. Check your Entity classes, they should have a collection property for one of the classes you need.

Categories