Show Multiple models data in one view [duplicate] - html

This question already has answers here:
Multiple models in a view
(12 answers)
Closed 4 years ago.
My project is based on Asp.net Mvc i have facing the issue to show multiple model data in one view but didn't show in list or grid view it should be show in Form View style
i have two modals class Product , BusinessCase
public class productViewModel
{
public string ProductTitle { get; set; }
public string Purpose { get; set; }
public string Composition { get; set; }
public string Requirment { get; set; }
}
business case viewModel
public class BcaseViewModel
{
public string ExecutiveSummary { get; set; }
public string Reason { get; set; }
public string ExpectedBenefits { get; set; }
public string DisBenefits { get; set; }
public string Cost { get; set; }
public string MajorRisks { get; set; }
}
I want to show these two view model data in one view that is
public ActionResult PBView()
{
return View();
}

Create a new ViewModel that contains your ViewModels and use it.
public class customViewModel{
{
public productViewModel productViewModel {get; set;}
public BcaseViewModel BcaseViewModel {get; set;}
}

Related

How can I get a JOIN result from a dbcontext class to format and to send as a JSON string?

I'm doing some practice with Entity Framework and I want to create a web-api backend, which is able to manage requests by interacting with a MySql database and by responding with JSON strings in the message body of the replies. I'm quite new to it and I'm trying to learn it solving problem by problem in the project.
An example of the working code at the moment is:
public string getFilmById(int id)
{
return JsonSerializer.Serialize(ctx.Films.ToList().Where(filmToGet => filmToGet.Id == id));
}
Which generates the string:
[{"Id":7,"Title":"Dune","Genre":"Sci-fi","Duration":155,"Direction":[],"Interpretation":[]}]
Now, what I want to do is to include in the string the directors and the actors of the film. I've created all the models of the backend with a database-first approach, by importing and converting MySql tables into classes and by generating the dbcontext class automatically. This implies that I also have the Direction class to link Film and Director, because the relationship is Many to Many (the same is for Interpretation and Actor). Here the code:
public partial class Film
{
public Film()
{
Direction = new HashSet<Direction>();
Interpretation = new HashSet<Interpretation>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Genre { get; set; }
public int? Duration { get; set; }
public virtual ICollection<Direction> Directions { get; set; }
public virtual ICollection<Interpretation> Interpretations { get; set; }
}
public partial class Direction
{
public int Id { get; set; }
public int? IdDirector { get; set; }
public int? IdFilm { get; set; }
public virtual Film IdFilmNavigation { get; set; }
public virtual Director IdDirectorNavigation { get; set; }
}
public partial class Director
{
public Director()
{
Directions = new HashSet<Direction>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public virtual ICollection<Direction> Directions { get; set; }
}
In the end: how can I include Directors and Actors in the Film instance and make them appear in that json string? Technically this is a simple JOIN after all, but I don't really know how to do it using these classes.
Thank you for the help!
Here's the repo with all the code if can help:
https://github.com/marco-savino/film_archive_project.git

Dotnet Core DB Scaffold creates additional Collections for Object

I have a Mysql Database and want to create a scaffold to try a data-first .net project. I got the scaffold to work, which was great, but it is creating these false Collections on my model objects for related tables.
For my coin object below, I only have the 5 fields on the top, but it is creating the collections to any of the tables where Coin is a foreign key. This is not really making any sense to me. I will never populate this data and cannot see any settings to stop this from happening.
public partial class Coin
{
public Coin()
{
LedgerTransactions = new HashSet<LedgerTransactions>();
Price = new HashSet<Price>();
TradeFeeCoin = new HashSet<Trade>();
TradeForCoin = new HashSet<Trade>();
TradeTradeCoin = new HashSet<Trade>();
}
[Column(TypeName = "int(11)")]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(5)]
public string Symbol { get; set; }
public byte? SortOrd { get; set; }
[Column(TypeName = "bit(1)")]
public bool? IsBaseCurrency { get; set; }
[InverseProperty("Coin")]
public ICollection<LedgerTransactions> LedgerTransactions { get; set; }
[InverseProperty("Coin")]
public ICollection<Price> Price { get; set; }
[InverseProperty("FeeCoin")]
public ICollection<Trade> TradeFeeCoin { get; set; }
[InverseProperty("ForCoin")]
public ICollection<Trade> TradeForCoin { get; set; }
[InverseProperty("TradeCoin")]
public ICollection<Trade> TradeTradeCoin { get; set; }
}
I created this table from a Code-First approach, and that model only had the 5 fields in it that I would expect. Am I doing something wrong?

Populate DropDown from database in an edit view using MVC4

I am new to MVC and trying to populate a dropdown list in the "create" view which is generated from a view model, but it returns with an error saying object reference is not an instance of an object. below is my code :
Controller Code:
public ActionResult Create()
{
return View(new AddRecipeViewModel());
}
Model Code:
public class DifficultyLevel
{
[Key]
public int Id { get; set; }
public string Difficulty { get; set; }
}
public class AddRecipeViewModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<RecipeReview> Reviews { get; set; }
public virtual IEnumerable<DifficultyLevel> Difficulty { get; set; }
}
View:
<div>
<select>
#foreach (var item in Model.Difficulty)
{
<option>#item.Difficulty</option>
}
</select>
</div>
Is there an easy workaround this ? as I will be adding more drop downs in this as I go along.
Thanks,
Vishal
not sure if you need to use virtual in your view models.. that's usually only for the entity models. but anyway, try adding a constructor to AddRecipeViewModel and set the collections equal to empty lists so they won't be null.
public class AddRecipeViewModel
{
public AddRecipeViewModel()
{
Reviews = new List<RecipeReview>();
Difficulty = new List<DifficultyLevel>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<RecipeReview> Reviews { get; set; }
public virtual IEnumerable<DifficultyLevel> Difficulty { get; set; }
}

How to make ICollection<Child Entities> Required. How

Here is my Master Entity who will contains a list of Language
public partial class WebSite
{
public WebSite()
{
this.WebSiteLanguages = new HashSet<WebSiteLanguage>();
}
public int Id { get; set; }
public Nullable<int> WLUserID { get; set; }
public string DomainName { get; set; }
public Nullable<bool> IsActive { get; set; }
//[Required]
public virtual ICollection<WebSiteLanguage> WebSiteLanguages { get; set; }
}
My WebSiteLanguage Child class is
public partial class WebSiteLanguage
{
public int Id { get; set; }
public string LanguageName { get; set; }
public Nullable<int> WebSiteID { get; set; }
public bool IsDefault { get; set; }
public virtual WebSite WebSite { get; set; }
}
In my View, I can Add many language as I want within an ajax call.
My Question is :
Is it possible to make the
public virtual ICollection WebSiteLanguages { get;
set; }
Required. The Website Entity is not valid if there is no WebSiteLanguage created.
Thanks a lot.
As per post http://blogs.msdn.com/b/adonet/archive/2011/05/27/ef-4-1-validation.aspx navigation properties are excluded from facet validation "as you could set the associated FK value and the navigation property would be set on SaveChanges()". To validate that a navigation property is not null you can:
create a custom attribute that validates it (be it on the type or on the property)
implement IValidatableObject interface that does the above
override DbContext.ValidateEntity protected method so that it validates that the property is not null and if this is the case calls base.ValidateEntity() to validate other properties (see this for more details: http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx)
The 3rd solution seems to be the cleanest.

Mapping complex tree object using fluent mappings in EF 4.1?

I need some assistance with how to properly mapping this structure in EF 4.1:
public class Menu: Entity
{
public string Title { get; set; }
public virtual ICollection<MenuItem> MenuItems { get; set; }
}
public class MenuItem: Entity
{
public string Icon { get; set; }
public string Text { get; set; }
public string Action { get; set; }
public string Controller { get; set; }
public string Parameters { get; set; }
public virtual MenuItemType Type { get; set; }
public virtual Guid? ContextMenuId { get; set; }
public virtual Menu ContextMenu { get; set; }
public virtual Guid? ParentMenuItemId { get; set; }
public virtual MenuItem ParentMenuItem { get; set; }
public virtual ICollection<MenuItem> ChildMenuItems { get; set; }
}
The Entity base class has the ID for the enitties and I also have a base mapping class that builds the mappings for the key. Here is what I have so far for the MenuItem class:
public class MenuItemMapping : EntityConfiguration<MenuItem>
{
public MenuItemMapping()
{
HasOptional(mi => mi.ParentMenuItem).WithMany(p => p.ChildMenuItems).HasForeignKey(mi => mi.ParentMenuItemId).WillCascadeOnDelete(false);
HasOptional(mi => mi.ContextMenu).WithMany().HasForeignKey(mi => mi.ContextMenuId).WillCascadeOnDelete(false);
}
}
My concern is in the ContextMenu because it is a Menu type and not sure the best way to handle this type o mapping.
Update
Well, I added an additional mapping for the Menu (in a MenuMapping class similar to the above mapping class) for the collection of Menuitems and it seems to be OK, but I'd still like to know if what I am doing is correct.
Apparently, my mappings were fine. I thought I would have issues with circular references.