2 models in a single view with if condition - c#

I have problem with how to and where to add if condition in view which has two models in a single view.
This is view
#foreach (service_provider SP in ViewBag.service_provider) {
<tr>
<td>#SP.Sp_email</td>
<td>#SP.Sp_name</td>
<td>#SP.city.Cityname</td>
</tr>}#foreach (picture img in ViewBag.pictures){
<tr>
<td><img src="data:image/png;base64,#Convert.ToBase64String(img.pic,0,img.pic.Length)" width="100" />
</td>
</tr>
}
This is picture model
public int PIC_ID { get; set; }
public string pic_name { get; set; }
public Nullable<int> belong_id { get; set; }
public byte[] pic { get; set; }
This is service_provider model
public int SPID { get; set; }
public string Sp_email { get; set; }
public string Sp_password { get; set; }
public string Sp_name { get; set; }
This is my controller
public ActionResult Index(){
ViewBag.service_provider = dc.service_provider;
ViewBag.pictures = dc.pictures;
return View();
}
The purpose of the above view is to display details of the service_provider with it's picture by where belong_id in picture equals SPID in service_provider. but I couldn't understand where do I add if condition.I'm using Database first approach

It would be much easier if you used a strongly typed model instead of the ViewBag. You can then add logic to your model or your controller. I personally it easier read and test. It also prevents syntax errors.
Models.cs
public class Picture {
public int PIC_ID { get; set; }
public string pic_name { get; set; }
public Nullable<int> belong_id { get; set; }
public byte[] pic { get; set; }
// added - your FK pointing back to the corresponding service_provider
public int SPID { get; set; }
// added - the corresponding service_provider
public service_provider ServiceProvider { get; set; }
}
public class service_provider {
public int SPID { get; set; }
public string Sp_email { get; set; }
public string Sp_password { get; set; }
public string Sp_name { get; set; }
// added
public ICollection<Picture> Pictures {get;set;}
}
Controller.cs
public ActionResult Index(){
var service_provider = dc.service_provider.Inculde(x => x.Pictures);
// var pictures = dc.pictures;
// code to add the pictures to the correct service_provider instance
// ideally this should already be reflected in your model.
// If you are using EF you should be modeling this using proper data relations
// that would allow you to execute an .Include statement on the retrieval
// and pass the model directly into the View without the need for an additional call to pictures as the service_provider would then already contain the relations
return View(service_provider);
}
View.cshtml
#model IEnumerable<service_provider>
#*This following line indicates that it is a list of service_provider hense the IEnumerable*#
#foreach (service_provider SP in Model){
<tr>
<td>#SP.Sp_email</td>
<td>#SP.Sp_name</td>
<td>#SP.city.Cityname</td>
</tr>
#*Iterate over the pictures on each model.
You can still split this if you want all service providers and then all pictures.*#
#foreach (picture img in SP.Pictures){ #*changed to still look at the model*#
<tr><td><img src="data:image/png;base64,#Convert.ToBase64String(img.pic,0,img.pic.Length)" width="100" /></td></tr>
}
}

Try:
#foreach (picture img in ViewBag.pictures)
{
if(img.PIC_ID == ViewBag.service_provider.SPID)
{
<tr><td><img src="data:image/png;base64,#Convert.ToBase64String(img.pic,0,img.pic.Length)" width="100" /></td></tr>
}
}
BTW - Maybe better way - is check in server side and return ONLY pics that you need??

Related

ASP.NET MVC Viewmodel returns nothing

Trying to make a simple application but my view returns nothing when trying to use a viewmodel. I assume the "db.[TableName].ToList();", which works when applied on a domain model, is not enough and the selection should happen in a different way when using a viewmodel, but I have no idea how to do it. Please help. Thank you.
Town.cs
using System.Collections.Generic;
namespace City.Models
{
public class Town
{
public Town()
{
Streets = new List<Street>();
}
public int TownId { get; set; }
public string TownName { get; set; }
public virtual ICollection<Street> Streets { get; set; }
}
}
Street.cs
using System.Collections.Generic;
namespace City.Models
{
public class Street
{
public Street()
{
Houses = new List<House>();
}
public int StreetId { get; set; }
public string StreetName { get; set; }
public virtual ICollection<House> Houses { get; set; }
}
}
House.cs
namespace City.Models
{
public class House
{
public int HouseId { get; set; }
public string HoueseName { get; set; }
public int StreetId { get; set; }
public virtual Street Street { get; set; }
}
}
Floor.cs
namespace City.Models
{
public class Floor
{
public int FloorId { get; set; }
public int FloorNumber { get; set; }
public int FireExtinguisherId { get; set; }
}
}
FireExtinguisher.cs
namespace City.Models
{
public class FireExtinguisher
{
public int FireExtinguisherId { get; set; }
public string FireExtinguisherName { get; set; }
public int FloorId { get; set; }
}
}
MyViewModel.cs
namespace City.Models
{
public class MyViewModel
{
public MyViewModel()
{
Town = new Town();
Street = new Street();
House = new House();
Floor = new Floor();
FireExtinguisher = new FireExtinguisher();
}
public int MyViewModelId { get; set; }
public Town Town { get; set; }
public Street Street { get; set; }
public House House { get; set; }
public Floor Floor { get; set; }
public FireExtinguisher FireExtinguisher { get; set; }
}
}
ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Town> Towns { get; set; }
public DbSet<Street> Streets { get; set; }
public DbSet<House> Houses { get; set; }
public DbSet<Floor> Floors { get; set; }
public DbSet<FireExtinguisher> FireExtinguishers { get; set; }
public DbSet<MyViewModel> MyViewModels { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
HomeController.cs (I think the problem lies here)
using System.Linq;
using System.Web.Mvc;
using City.Models;
namespace City.Controllers
{
public class HomeController : Controller
{
private ApplicationDbContext db;
public HomeController()
{
db = new ApplicationDbContext();
}
public ActionResult Index()
{
return View(db.MyViewModels.ToList());
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
Index.cshtml
#model IEnumerable<City.Models.MyViewModel>
<h2>Map information</h2>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Town</th>
<th>Street</th>
<th>House</th>
<th>Floor</th>
<th>FireExtinguisher</th>
</tr>
</thead>
#foreach (var item in Model)
{
<tbody>
<tr>
<td>#(item.Town.TownName)</td>
<td>#(item.Street.StreetName)</td>
<td>#(item.House.HoueseName)</td>
<td>#(item.Floor.FloorNumber)</td>
<td>#(item.FireExtinguisher.FireExtinguisherName)</td>
</tr>
</tbody>
}
</table>
</div>
Even though I have test data in the db, this is all what I see when I run it:
Image is here
Please tell me what should I fix, how to get data retrieved. Thanks
EDIT #CrowdCoder
new picture here
I think your understanding about view model is incorrect.
View model is a class to transfer data between your view and your action method. View model is specific to the view. So if your view needs to display only 2 properties (Name and Age), your view model will have only those 2 properties. No need to bring all the properties from your entity model to the view model class.
I see that you added a new collection to the your db context,
public DbSet<MyViewModel> MyViewModels { get; set; }
This does not makes any sense. As i mentioned earlier, view models are UI concerns. It should not be in your data access code. Also do not mix the entities created by your ORM layer in your view model.
Also view models are simple POCOs. It should be lean-flat classes with properties. It is your responsibility to load the property values. You can do that in your action method or another method called from your action method.
Let's say you want to display a list of houses with it's street name, you will create a view model like this
public class HouseViewModel
{
public int HouseId { set; get;}
public string HoueseName { set;get;}
public string StreetName { set;get; }
}
Now in your view, you simply access these properties
#model IEnumerable<HouseViewModel>
<table>
#foreach(var item in Model)
{
<tr>
<td>#item.HouseId </td>
<td>#item.HoueseName </td>
<td>#item.StreetName </td>
</tr>
}
</table>
Ofcourse, for this code to work, you need to make sure you will be creating a list of HouseViewModel and send it to the view from your action method.
public ActionResult Index()
{
var list= new List<HouseViewModel>{
new HouseViewModel { HouseId =1,HoueseName ="Abc", StreetName ="Detroit"},
new HouseViewModel { HouseId =2,HoueseName ="Xyz", StreetName ="Novi"}
};
return View(list);
}
Now you can see that how we are using view model to transfer data from the action method to the view. Here we just hard coded the property values for the items in the list we are sending. We can update that to read from your EF db context as needed.
Let's read all the Houses, use LINQ projection to create a HouseViewModel object for each item in that collection and assign the property values.
public ActionResult Index()
{
var houses = db.Houses
.Select(a=>new HouseViewModel
{ HouseId =a.HouseId,
HoueseName =a.HouseName,
StreetName = a.Street.StreetName
})
.ToList();
return View(houses);
}

Listing all Items under a Category C# MVC

I would like some direction on how to be able to list all the items under a certain category in a view. I have already searched around and can't find anything, If someone know of any other posts or articles please point me there. Other wise here is my model...
public class Category
{
[Key]
public int ID { get; set; }
[Display(Name = "Category")]
public string Name { get; set; }
public virtual ICollection<MenuItem> MenuItem { get; set; }
}
public class MenuItem
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public string Image { get; set; }
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
}
Thanks for any help anyone can provide.
assuming your category is set at the model
#foreach(var item in Model.MenuItem)
{
#Html.HiddenFor(x => item.ID)
<div>#Html.DisplayFor(x => item.Name)</div>
// carry on with other properties / markup
}
Say you want to loop over the MenuItem property in the Category model, in your view you'd have something like this:
At the top of the View, you'd have this:
#model Category
#foreach (var item in Model.MenuItem)
{
<tr id="menu-item">
<td>#id</td>
<td>#Name</td>
<td>#Description</td>
<td>#Image</td>
</tr>
}
#foreach (var item in Model.MenuItem)
{
<tr>
<td>#Name</td>
<td>#Description</td>
</tr>
{

ASP.Net MVC Pass EF Data to a Model

I am new to MVC and Entity Framework, and would like some assistance in passing data to a model so that I can make my view a strongly typed view and get the information out as I need.
In the about action in the home controller i want to pull out data from my SiteContent table that is for the About Page
var sc = db.siteContents.Where( x => x.Page == "About").ToList();
return View(sc);
A couple of questions I have around this, is first, how do I get this into my Model SiteContent
public class SiteContent
{
public int ContentId { get; set; }
public string ContentType { get; set; }
public string Page { get; set; }
public string ContentHeader { get; set; }
public string Content { get; set; }
public DateTime OriginalPostDate { get; set; }
public DateTime UpdatePostDate { get; set; }
public string ImageFileName { get; set; }
public bool ContentReleased { get; set; }
}
I am uncertain on how I should proceed from here.
The from here I would like to use a strongly typed view to get the data by using my model.
Any and all help very much appreciated.
Thanks
Simon
Define your view like this:
#model IEnumerable<SiteContent>
<table>
<tr>
<td>ContentType</td>
// other data
</tr>
#foreach(var item in Model)
{
<tr>
<td>#item.ContentType</td>
// other data
<tr>
}
</table>

Asp.net two models in one view

I'm trying to get 2 models to show in 1 view but it is not working. I have tried lots of different ideas from Google but none have worked so far.
There is no error in the Error List. But when I start it I get this error.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Namespace.Models.Class1]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Namespace.Models.ParentClass]'.
I have a parent class containing the to child classes. If i use the #model IEnumerable<> directly at the child class it works, but not when pointed at the parent.
What am I doing wrong?
EDIT
Ok so these are my files.
Model1.cs
public int MyProperty1 { get; set; }
public int MyProperty2 { get; set; }
Model2.cs
public int AnotherProperty1 { get; set; }
public int AnotherProperty2 { get; set; }
ViewModel.cs
public IEnumerable<Model1> Model1 { get; set; }
public IEnumerable<Model2> Model2 { get; set; }
HomeController.cs
private ConnectContext db = new ConnectContext();
public ActionResult Index()
{
var model = from m in db.model select m;
model = db.model.OrderByDescending(m => m.ID);
return View(db.model.ToList());
}
Index.chstml
#model IEnumerable<Namespace.Models.ViewModel>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Model1.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Model2.Title)
</td>
</tr>
}
Now with the files like this my error message is
CS1061: 'System.Collections.Generic.IEnumerable<Namespace.Models.Model1>' does not contain a definition for 'Cover' and no extension method 'Cover' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Namespace.Models.Model1>' could be found (are you missing a using directive or an assembly reference?)
If you have two and only two classes you want to pass in, have you considered using a tuple?
For example:
On the controller end,
var model = new Tuple<ModelType1, ModelType2>(yourModel1, yourModel2);
return View(model);
On the view end, you'll want this at the top, along with any using statements you may need:
#model Tuple<ModelType1, ModelType2>
To access each part in the view, #Model.Item1 will be your ModelType1 and #Model.Item2 will be your ModelType2.
If you wind up with more than two classes, it might be a good idea for you to make a ViewModel class with properties for the various types you want to include. (You can also cop out and add properties to the ViewBag.)
What about just making a model class with properties that makes up the two classes you need for your view?
E.g.
public class FirstModel
{
public int Id { get; set; }
public string SomeProperty { get; set; }
}
public class SecondModel
{
public int Id { get; set; }
public string SomeOtherProperty { get; set; }
}
public class ViewModel
{
public FirstModel MyFirstModel { get; set; }
public SecondModel MySecondModel { get; set; }
}
Then in your view you use a model of ViewModel.
Try this:
public class FirstModel
{
public int Id { get; set; }
public string SomeProperty { get; set; }
}
public class SecondModel
{
public int Id { get; set; }
public string SomeOtherProperty { get; set; }
}
public class ViewModel
{
public FirstModel MyFirstModel { get; set; }
public SecondModel MySecondModel { get; set; }
}
In your Controller:
private ConnectContext db = new ConnectContext();
public ActionResult Index()
{
FirstModel firstModel = //Set FirstModel Value;
SecondModel secondModel = //Set SecondModel Value;
ViewModel viewModel = new ViewModel(){
FirstModel = firstModel,
SecondModel = secondModel
}
return View(viewModel);
}
Finally I got this working. I had to change my ViewModel, Controller and View.
ViewModel.cs (From IEnumerable to List)
public List<Model1> Model1 { get; set; }
public List<Model2> Model2 { get; set; }
HomeController.cs
private ConnectContext db = new ConnectContext();
public ActionResult Index()
{
ViewModel vm = new ViewModel();
vm.Model1 = (from m in db.Model1 select m).OrderByDescending(x => x.ID).Take(3).ToList();
vm.Model2 = (from t in db.Model2 select t).OrderByDescending(x => x.ID).Take(3).ToList();
return View(vm);
}
Index.cshtml
(So here I removed the IEnumerable and then each Foreach connects to each Model)
#model Namespace.Models.ViewModel
#foreach (var item in Model.Model1) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
</tr>
}
#foreach (var item in Model.Model2) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
</tr>
}

how to handle a list of checkbox?

I am developing a page which display data from service on the View and allow users to filter it.
There is a column for Countries, which allow users to filter.
I am not able to figure out a way to create a list of checkbox so that I can grab all selected values in one param like string[] countries (in the action method).
Can't use classic way :
<input type="checkbox" name="countries" value="USA" />USA<br />
<input type="checkbox" name="countries" value="Canada" />Canada<br />
This does pass the values in URL but doesn't set them back on post back (keeping checked onpostback).
I tried using checkboxlist (http://goo.gl/TUvZzu) but seemed to complicated for my Modal.
Since mine is a very straight forward Model :
public string Title { get; set; }
public string Website { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Zip { get; set; }
public string State { get; set; }
public string Country { get; set; }
I appreciate your time and help.
You need to include a collection of countries within your view model to hold the selected values and allow them to be sent in the post.
I would also create a Country object to hold the Id, Name and Selected values.
In order for the model to be posted back you need to index each item in the view, this allows the model binder to pick it up.
Models
public class AModel
{
public AModel()
{
Countries = new List<Country>();
}
public string Title { get; set; }
public string Website { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Zip { get; set; }
public string State { get; set; }
public string Country { get; set; }
// Country collection
public List<Country> Countries { get; set; }
}
public class Country
{
public int ID { get; set; }
public string Name { get; set; }
public bool Checked { get; set; }
}
View for loop
#for(var i = 0; i < Model.Countries.Count; i++)
{
<dt>
#Html.HiddenFor(m => Model.Countries[i].ID)
#Html.HiddenFor(m => Model.Countries[i].Name)
#Html.CheckBoxFor(m => Model.Countries[i].Checked)
</dt>
<dd>
#Model[i].Name
</dd>
}
Note the for loop instead of foreach to enable model binding and the hidden fields to allow the values to be posted back to the controller
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
Controller post
[HttpPost]
public ActionResult Index(AModel model)
{
//All the selected countries are available in the model
return View(model);
}
Working example

Categories

Resources