Reputation: 1589
I am trying to just include who created or submitted a school request in a form. I am developing in C# MVC4 in visual studio 2012. I am a newbie with this mvc c# so i am sure this is easy for all.
here is my view details.cshtml:
@model Models.Schools
/*I get an error when i do this, it looks like i can only include one model, as school name, and address gets to be undefined.*/
@model Models.whoCreated
//this is coming from Models.Schools
school name:
address:
/*this is coming from Models.WhoCreated
Schools and WhoCreated are both tables in db and share common id.
any idea on how to achieve this? also i would like to show this field only to admin, and I am using Membership.getUser (all valid users are stored in my db, with admin or just regular user status)*/
created by:
Upvotes: 0
Views: 93
Reputation: 1785
You should create new model that contains all the fields that you will need on view. You need to know that tables in database and model you display are (or should be at least) two different classes, you get data from database and put it into model class that you display on your view.
So your model class should look like:
public class SchoolModel
{
public string SchoolName {get; set;}
public string Address {get;set;}
public string CreatedBy {get;set;}
}
and then in controller action:
public class HomeController : Controller
{
public ActionResult Index()
{
//getting info from database to variables
SchoolModel schoolModel = new SchoolModel();
schoolModel.SchoolName = //school retrieved from database, something like context.Schools.Name
schoolModel.Address = // something like context.Schools.Address
schoolModel.CreatedBy = // context.Users.Where(x => x.id == yourIdOrSomething)
return View(schoolModel);
}
}
then in default.cshtml
@model Models.SchoolModel
school: @Model.SchoolName
address: @Model.Address
created by : @Model.CreatedBy
Upvotes: 2