Im working with ASPNET MVC5 and i just Followed this tutorial : http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-grid-in-mvc-using-grid-mvc/ and it works fine.
I now want to update my HomeController with 2 tables (Users & Roles),
Note : My Datacontext contains 2 tables (Users & Roles)
HomeController.cs
public ActionResult Details()
{
DataClassesUserDataContext db = new DataClassesUserDataContext();
var SchemeList = from d in db.AspNetUsers
join i in db.AspNetUserRoles
on d.Id equals i.UserId
select new { AspNetUsers = d, AspNetUserRole = i };
return View(SchemeList);
}
I return good my results
Now in my View :
My Details.cshtml
Is the probleme here ? can i call shanuMVCUserRoles.DB.AspNetUser & AspNetUserRoles ???
#model IEnumerable <shanuMVCUserRoles.DB.AspNetUser>
// possible to add other models here ? #model IEnumerable <shanuMVCUserRoles.DB.AspNetUserRole >
> <div
> class="code-cut">
> #Html.Grid(Model).Columns(columns => {
> columns.Add(c => c.UserName).Titled("UserName").Filterable(true);
> columns.Add(c => c.Id).Titled("ID").Filterable(true);
> columns.Add(c => c.PasswordHash).Titled("PasswordHash").Filterable(true);
> columns.Add(c => c.Email).Titled("Email").Filterable(true);
> columns.Add(c => c.AccessFailedCount).Titled("AccessFailedCount").Filterable(true);
> columns.Add(c => c.PhoneNumber).Titled("PhoneNumber").Filterable(true);
> columns.Add()
> .Encoded(false)
> .Sanitized(false)
> .SetWidth(30)
> .RenderValueAs(o => Html.ActionLink("Edit", "Edit", new { id = o.Id })); }).WithPaging(10).Sortable(true) </div>
i have the following error :
The model item passed into the dictionary is of type 'System.Data.Linq.DataQuery1[<>f__AnonymousType42[shanuMVCUserRoles.DB.AspNetUser,shanuMVCUserRoles.DB.AspNetUserRole]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[shanuMVCUserRoles.DB.AspNetUser]'.
you could workaround the problem by putting your AspNetUserRoles collection into ViewBag. This link shows this solution among others:
http://www.codeproject.com/Articles/687061/Multiple-Models-in-a-View-in-ASP-NET-MVC-MVC
Personally, I consider the Model as a kind of contract between my Controller and my View, so I do not like putting business information outside the Model. For example, it would be ok to store a string to format DateTimes into the ViewBag. But if I want a Controller and a View to manage Users and Roles, I want it to be clear for me and for anybody else reading the code, which can happen today, tomorrow or in ten years.
So in this case I would create a Model class:
namespace WebApplication1.Models
{
public class UsersAndRolesModel
{
public IList<shanuMVCUserRoles.DB.AspNetUser> Users { get; set; }
public IList<shanuMVCUserRoles.DB.AspNetRole> Roles { get; set; }
}
}
I would create it and populate it in my Controller.
Then in my View I would declare:
#model WebApplication1.Models.UsersAndRolesModel
and finally my grids:
#Html.Grid(Model.Users).Columns(columns => { // and so on...
#Html.Grid(Model.Roles).Columns(columns => { // and so on...
I hope this helps.
Editing:
Hi David, it depends on what you actually want to do: do you want a View showing two tables, one for all the users and one for all the roles?
In this case, a first implementation could (but should not) be:
public ActionResult Details()
{
UsersAndRolesModel model = new UsersAndRolesModel();
DataClassesUserDataContext db = new DataClassesUserDataContext();
model.Users = db.AspNetUsers.ToList();
model.Roles = db.AspNetUserRoles.ToList();
return View(model);
}
I wrote should not be because DataClassesUserDataContext must be disposed after usage. So what you really should do is:
public class HomeController : Controller
{
private DataClassesUserDataContext db = new DataClassesUserDataContext();
public ActionResult Details()
{
UsersAndRolesModel model = new UsersAndRolesModel();
model.Users = db.AspNetUsers.ToList();
model.Roles = db.AspNetUserRoles.ToList();
return View(model);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
I have edited my previous answer with some information about how the Controller could be implemented.
The NullReferenceException happens because returning
return View();
provides a null model to the View, so Model.Users and Model.Roles cause the NullReferenceException in your View.
Please note that I am returning
return View(model);
in my HomeController.
Just let me add that I avoid using DbContexts in my controllers.
I prefer implementing the Repository pattern.
You can find a nice example here:
http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
It is a better approach because it allows you to create a Test project and to test your controllers by providing mock repository implementations.
Related
I have a model containing a couple of lists:
[Display(Name = "Facilities")]
public List<facility> Facilities { get; set; }
[Display(Name = "Accreditations")]
public List<accreditation> Accreditations { get; set; }
I populate these lists initially from my controller:
public ActionResult Register()
{
var viewModel = new RegisterModel();
viewModel.Facilities = m_DBModel.facilities.ToList();
viewModel.Accreditations = m_DBModel.accreditations.ToList();
return View(viewModel);
}
When they get to my view they are populated with the DB records (great). I then pass the model to the partial view which displays these lists as checkboxes, ready for user manipulation (I have tried based on another suggestion using for loop instead of foreach loop, made no difference):
#model LanguageSchoolsUK.Models.RegisterModel
#foreach (var item in Model.Facilities)
{
#Html.Label(item.name);
#Html.CheckBox(item.name, false, new { id = item.facility_id, #class = "RightSpacing", #description = item.description })
}
When I submit the form and it ends up back at my controller this time calling the overloaded register function on the controller:
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Do stuff
}
return View(model);
}
The problem is that the model parameter containing the lists (Facilities and Accreditations) is telling me that the lists are null.
Please can somebody tell me what I am doing wrong, why aren't they populated with the collections that I originally passed through and hopefully a way of asking whick ones have been checked?
Thanks.
I have tried based on another suggestion using for loop instead of
foreach loop, made no difference
Try again, I am sure you will have more luck this time. Oh and use strongly typed helpers:
#model LanguageSchoolsUK.Models.RegisterModel
#for (var i = 0; i < Model.Facilities.Count; i++)
{
#Html.HiddenFor(x => x.Facilities[i].name)
#Html.LabelFor(x => x.Facilities[i].IsChecked, Model.Facilities[i].name);
#Html.CheckBoxFor(
x => x.Facilities[i].IsChecked,
new {
id = item.facility_id,
#class = "RightSpacing",
description = item.description // <!-- HUH, description attribute????
}
)
}
Also you will undoubtedly notice from my answer that checkboxes work with boolean fields on your model, not integers, not decimals, not strings => BOOLEANS.
So make sure that you have a boolean field on your model which will hold the state of the checkbox. In my example this field is called IsChecked but obviously you could feel absolutely free to find it a better name.
I am teaching myself asp .net mvc3. I have researched a lot but the more I read the more confused I become. I want to create a page where users can register their property for sale or rent.
I have created a database which looks like this:
public class Property
{
public int PropertyId { get; set; }
public int PropertyType { get; set; }
ยทยทยท
public int Furnished { get; set; }
...
}
Now, I want dropdownlistfor = PropertyType and Furnished.
Property type would be
1 Flat
2 House
3 Detached House
...
Furnished would be:
1 Furnished
2 UnFurnished
3 PartFurnished
...
Now, I am really not sure where to keep this information in my code. Should I have 2 tables in my database which store this lookup? Or should I have 1 table which has all lookups? Or should I just keep this information in the model?
How will the model bind to PropertyType and Furnished in the Property entity?
Thanks!
By storing property types and furnished types in the database, you could enforce data integrity with a foreign key, rather than just storing an integer id, so I would definitely recommend this.
It also means it is future proofed for if you want to add new types. I know the values don't change often/will never change but if you wanted to add bungalow/maisonette in the future you don't have to rebuild and deploy your project, you can simply add a new row in the database.
In terms of how this would work, I'd recommend using a ViewModel that gets passed to the view, rather than passing the database model directly. That way you separate your database model from the view, and the view only sees what it needs to. It also means your drop down lists etc are strongly typed and are directly in your view model rather than just thrown into the ViewBag. Your view model could look like:
public class PropertyViewModel
{
public int PropertyId { get; set; }
public int PropertyType { get; set; }
public IEnumerable<SelectListItem> PropertyTypes { get; set; }
public int Furnished { get; set; }
public IEnumerable<SelectListItem> FurnishedTypes { get; set; }
}
So then your controller action would look like:
public class PropertiesController : Controller
{
[HttpGet]
public ViewResult Edit(int id)
{
Property property = db.Properties.Single(p => p.Id == id);
PropertyViewModel viewModel = new PropertyViewModel
{
PropertyId = property.Id,
PropertyType = property.PropertyType,
PropertyTypes = from p in db.PropertyTypes
orderby p.TypeName
select new SelectListItem
{
Text = p.TypeName,
Value = g.PropertyTypeId.ToString()
}
Furnished = property.Furnished,
FurnishedTypes = from p in db.FurnishedTypes
orderby p.TypeName
select new SelectListItem
{
Text = p.TypeName,
Value = g.FurnishedTypeId.ToString()
}
};
return View();
}
[HttpGet]
public ViewResult Edit(int id, PropertyViewModel propertyViewModel)
{
if(ModelState.IsValid)
{
// TODO: Store stuff in the database here
}
// TODO: Repopulate the view model drop lists here e.g.:
propertyViewModel.FurnishedTypes = from p in db.FurnishedTypes
orderby p.TypeName
select new SelectListItem
{
Text = p.TypeName,
Value = g.FurnishedTypeId.ToString()
};
return View(propertyViewModel);
}
}
And your view would have things like:
#Html.LabelFor(m => m.PropertyType)
#Html.DropDownListFor(m => m.PropertyType, Model.PropertyTypes)
I usually handle this sort of situation by using an enumeration in code:
public enum PropertyType {
Flat = 1,
House = 2,
Detached House = 3
}
Then in your view:
<select>
#foreach(var val in Enum.GetNames(typeof(PropertyType)){
<option>val</option>
}
</select>
You can set the id of the option equal to the value of each item in the enum, and pass it to the controller.
EDIT: To directly answer your questions:
You can store them as lookups in the db, but for small unlikely to change things, I usually just use an enum, and save a round trip.
Also look at this approach, as it looks better than mine:
Converting HTML.EditorFor into a drop down (html.dropdownfor?)
I need to know how I could create a drop down list to represent all the categories in my "Categories" table.
I have already extracted the names and the values of each category I need, using this LINQ query :
var dbcontext = new LNQ2SQLDataContext();
var Q = from P in dbcontext.Categories
where P.SUB_CAT == null
select P;
I can pass this "Q" to my view like this :
In Controller :
return View(Q);
And in the View :
#model IEnumerable<MyAppName.Models.Category>
But I have no idea how to use #html.DropDownListFor() to make a darn good drop down list out of the model. :|
PLUS:
I could make a SelectList from the query "Q" like this :
var category_list = new SelectList(Q, "CAT_ID", "CAT_Name");
BUT I don't know how to create a drop down list (without using ViewBag to pass the category_list to the view) from a simple SelectList, either :|
I searched through as many blogs and websites as I could. But they didn't have the solution for my problem. I only got more and more confused!
So can anybody help please ? :/
To use DropDownListFor you'll either have to have a model that has a SelectList or data to make a selectlist out of and a property to store the selected value of the dropdown OR use the ViewBag to pass the category_list. So you can go with...
Public Class MyViewModel
{
Public Integer SelectedCategory { get; set; }
Public SelectList Categories { get; set; }
}
Public Class ItemsController : Controller
{
Public ActionResult Index()
{
var dbcontext = new LNQ2SQLDataContext();
var Q = from P in dbcontext.Categories
where P.SUB_CAT == null
select P;
var vm = new MyViewModel();
vm.Categories = new SelectList(Q, "CategoryID", "Name");
return View(vm);
}
[HttpPost()]
Public ActionResult Index(MyViewModel vm)
{
var theSelectedCategory = vm.SelectedCategory;
}
}
The view would be...
#model MyViewModel
#Html.DropDownListFor(model => model.SelectedCategory, Model.Categories, "Select Category")
Note: I don't typically code in C# so I can't guarantee the syntax is exactly right.
I am trying to work out the best way of using a viewmodel in the case of creating a new object.
I have a very simple view model that contains a contact object and a select list of companies.
private ICompanyService _Service;
public SelectList ContactCompanyList { get; private set; }
public Contact contact { get; private set; }
public ContactCompanyViewModel(Contact _Contact)
{
_Service = new CompanyService();
contact = _Contact;
ContactCompanyList = GetCompanyList();
}
private SelectList GetCompanyList()
{
IEnumerable<Company> _CompanyList = _Service.GetAll();
return new SelectList(_CompanyList, "id", "name");
}
I then have contact controller that uses this viewmodel and enable me to select a related company for my contact.
[Authorize]
public ActionResult Create()
{
return View(new ContactCompanyViewModel(new Contact()));
}
My issue is with the create method on the controller.
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Contact _Contact)
{
try
{
_Service.Save(_Contact);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
The problem is that the view returns an empty contact object, but! the company id is populated, this is because the dropdown list explicitly declares its field name.
#Html.DropDownList("parent_company_id",Model.ContactCompanyList)
The standard html form fields pass the objects values back in the format of contact.forename when using the HTML.EditorFor helper...
#Html.EditorFor(model => model.contact.forename)
I can access them if I use a FormCollection as my create action method paremeter and then explicitly search for contact.value but I cannot use a Contact object as a parameter to keep my code nice and clean and not have to build a new contact object each time.
I tried passing the actual view model object back as a parameter but that simply blows up with a constructor error (Which is confusing seeing as the view is bound to the view model not the contact object).
Is there a way that I can define the name of the Html.EditFor field so that the value maps correctly back to the contact object when passed back to the create action method on my controller? Or Have I made some FUBAR mistake somewhere (that is the most likely explanation seeing as this is a learning exercise!).
Your view model seems wrong. View models should not reference any services. View models should not reference any domain models. View models should have parameterless constructors so that they could be used as POST action parameters.
So here's a more realistic view model for your scenario:
public class ContactCompanyViewModel
{
public string SelectedCompanyId { get; set; }
public IEnumerable<SelectListItem> CompanyList { get; set; }
... other properties that the view requires
}
and then you could have a GET action that will prepare and populate this view model:
public ActionResult Create()
{
var model = new ContactCompanyViewModel();
model.CompanyList = _Service.GetAll().ToList().Select(x => new SelectListItem
{
Value = x.id.ToString(),
Text = x.name
});
return View(model);
}
and a POST action:
[HttpPost]
public ActionResult Create(ContactCompanyViewModel model)
{
try
{
// TODO: to avoid this manual mapping you could use a mapper tool
// such as AutoMapper
var contact = new Contact
{
... map the contact domain model properties from the view model
};
_Service.Save(contact);
return RedirectToAction("Index");
}
catch
{
model.CompanyList = _Service.GetAll().ToList().Select(x => new SelectListItem
{
Value = x.id.ToString(),
Text = x.name
});
return View(model);
}
}
and now in your view you work with your view model:
#model ContactCompanyViewModel
#using (Html.BeginForm())
{
#Html.DropDownListFor(x => x.SelectedCompanyId, Model.CompanyList)
... other input fields for other properties
<button type="submit">Create</button>
}
If I were to pass additional data (other than the model) into my view, like say a list of files from a specific folder, whats the best way to do that?
I was thinking something like making a method and return a list into ViewData:
public List<string> GetFiles(int id, string cat)
{
var files = new List<string>();
var folder = "~/App_Data/uploads/" + cat + "/" + id.ToString();
foreach (var file in Directory.GetFiles(folder))
{
files.Add(file);
}
return files;
}
The controller:
ViewData["files"] = db.GetFiles(id, "random");
The view:
#foreach (var item in ViewData["files"]){ //markup }
First, ive heard that viewdata and viewbag shouldnt be used. Second, this code doesnt work. Something with Viewdata object is not enumerable. Should I make a model class for these files. If so, how? 2 models in one view? Im a bit confused how to do this proberly.
If I were to pass additional data (other than the model) into my view,
like say a list of files from a specific folder, whats the best way to
do that?
Write a view model which will contain all the properties your view needs and have your controller action pass this view model to the view. So:
public class MyViewModel
{
public List<string> Files { get; set; }
public string Foo { get; set; }
... some other properties that your view might need
}
then the controller:
public ActionResult Index(int id)
{
var model = new MyViewModel
{
Files = db.GetFiles(id, "random"),
Foo = "bar"
};
return View(model);
}
and finally in your strongly typed view:
#model MyViewModel
#foreach (var file in Model.Files)
{
<div>#file</div>
}
<div>#Html.DisplayFor(x => x.Foo)</div>