I have the following classes defined in my project
public class PolicyDocs
{
[Key]
[Required]
[MaxLength(20)]
public string PolicyDocID { get; set; }
[DisplayName("Date Revised")]
public DateTime DateRevised { get; set; }
[MaxLength(500)]
[DisplayName("Document Name")]
public string DocumentName { get; set; }
[MaxLength(10)]
[DisplayName("Version")]
public string VersionNo { get; set; }
[MaxLength(255)]
[DisplayName("Location")]
public string DocumentPath { get; set; }
public string CategoryID { get; set; }
public ICollection<PolicyDocumentsCatagory> PolicyDocumentsCatagory { get; set; }
}
and
public class PolicyDocumentsCatagory
{
[Key]
[MaxLength(20)]
//[Required]
public string CategoryID { get; set; }
[MaxLength(20)]
[Required]
[DisplayName("Description")]
public string CategoryDesc { get; set; }
[DisplayName("Active")]
public Boolean IsActive{ get; set; }
}
Data is properly loaded in to the table .
Example:
PolicyDocs:
1 ,"12-Jan-2017", "Document 1","version 1",100
PolicyDocumentsCategory:
100, "Information Technology", "true".
I am using the following query in my Controller Class
_context = new ApplicationDbContext();
//PolicyDocs _Policy = new PolicyDocs();
var _LPolicies = _context.PolicyDocs.Include(i=> i.PolicyDocumentsCatagory).ToList();
return PartialView("_GetAllPolicies", _LPolicies);
While running the query I am unable to see any data loaded for PolicyDocumentsCategory in "_LPolicies" object. The query is loading data in table "PolicyDocs".
Nor I am able to reference the PolicyDocumentsCategory in VIEW. I can not see the properties of "PolicyDocumentsCatagory" in the view
#model IEnumerable< MyRTOCloud.DBModels.PolicyDocs>
<div class="row">
<div>
<table id="Categories" class="table table-hover">
<thead>
<th>Category</th>
<th>Document Name</th>
<th>Release Date</th>
<th>Version</th>
</thead>
#foreach (var lp in Model)
{
<tr>
<td>#lp.PolicyDocumentsCatagory.____</td> // I can not see any peroprty of "PolicyDocumentsCatagory"
<td>#lp.DocumentName </td>
<td>#lp.DateRevised </td>
<td>#lp.VersionNo </td>
</tr>
}
</table>
</div>
</div>
Thanks,
PolicyDocumentsCatagory is a collection and you have to iterate it in itself or in your case you can combine with a separator as a string.
So, try something like that;
<td>#string.Join(",", lp.PolicyDocumentsCatagory.Select(x => x.CategoryID))</td>
Your PolicyDocs class is referencing a single CategoryID leading me to assume this is a one-to-many relationship (PolicyDocs being the many, PolicyDocumentsCatagory being the one). If that is the case, you will want to change your PolicyDocumentsCatagory property in PolicyDocs to public virtual PolicyDocumentsCatagory PolicyDocumentsCatagory { get; set; }. Note the virtual part. This is what allows EF to populate the relationship property automatically.
public class PolicyDocs
{
[Key]
[Required]
[MaxLength(20)]
public string PolicyDocID { get; set; }
[DisplayName("Date Revised")]
public DateTime DateRevised { get; set; }
[MaxLength(500)]
[DisplayName("Document Name")]
public string DocumentName { get; set; }
[MaxLength(10)]
[DisplayName("Version")]
public string VersionNo { get; set; }
[MaxLength(255)]
[DisplayName("Location")]
public string DocumentPath { get; set; }
public ICollection<PolicyDocumentsCatagory> PolicyDocumentsCatagory { get; set; }
}
public class PolicyDocumentsCatagory
{
[Key]
[MaxLength(20)]
public string CategoryID { get; set; }
[Required]
[MaxLength(20)]
public string PolicyDocID { get; set; }
[ForeignKey]
public PolicyDocs PolicyDocs { get; set; }
[MaxLength(20)]
[Required]
[DisplayName("Description")]
public string CategoryDesc { get; set; }
[DisplayName("Active")]
public Boolean IsActive{ get; set; }
}
Your models are not valid. You need to have a foreign key in your PolicyDocumentsCatagory that referencing to PolicyDocs and there is no need for CategoryID in PolicyDocs.
One PolicyDocs has many PolicyDocumentsCatagory, while one PolicyDocumentsCatagory is referenced to only one PolicyDocs. Standard one to many relationship type.
Related
I have customer model
public class Customer
{
public Customer()
{
this.SystemValues = new HashSet<SystemValue>();
}
public string Name { get; set; }
public Nullable<System.Guid> GUID { get; set; }
public int Id { get; set; }
public virtual ICollection<SystemValue> SystemValues { get; set; }
}
and systemValue model
public class SystemValue
{
public int CustomerId { get; set; }
public int SystemValueId { get; set; }
public Nullable<int> SystemValueCategoryId { get; set; }
public string SystemValueType { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string TextValue { get; set; }
public Nullable<int> IntValue { get; set; }
public Nullable<double> FloatValue { get; set; }
public byte[] BlobValue { get; set; }
public Nullable<System.DateTime> DateTimeValue { get; set; }
public Nullable<bool> BooleanValue { get; set; }
public Nullable<int> LookupValueId { get; set; }
public Nullable<int> LookupValueGroupId { get; set; }
public Nullable<bool> IsReadonly { get; set; }
public bool IsHidden { get; set; }
public int Id { get; set; }
public virtual Customer Customers { get; set; }
}
in which way I could show a link in CustomerView(CustomersController) foreach customer that redirect to the SystemValuesView(SystemValuesController) with related to this customer SystemValues?
I found out one way - redirect to this controller's action with parameter.
public ActionResult ViewSystemValues(int? id)
{
return RedirectToAction("Index", "SystemValues", new {id});
}
But I'm sure there must be smarter way.
#for(int i = 0; i < Model.yourcustomerlist.Count(); i++)
{
<tr>
<td>
<a class="btn btn-primary" href="#Url.Action("Index", "SystemValues", new { id = Model.yourcustomerlist[i].CustomerId })">
<b>Go to system values</b>
</a>
</td>
</tr>
}
I hope I understood you correctly.
This code should go in the view. The view should be strongly typed to a model.
Code is if you want a button that redirects to the index view of the SystemValues controller, with the CustomerId as input. You should change "yourcustomerlist" to the list containing the customer information. If it's not part of a table, remove the table-related tags (<td> and <tr>).
I’m trying to use complex models with Kendo grid edit popup. When submitting ALResults object properties are always null. It works fine when I’m not using Kendo. Is there a problem with kendo complex model submitting?
public class InitialApplicantLevel2Model
{
public InitialApplicantLevel2Model()
{
alResultsModel = new ALResults();
}
public int InitialApplicantLevel2ID { get; set; }
public string ApplicantName { get; set; }
public string ContactNumber { get; set; }
public string School { get; set; }
[Required(ErrorMessage="Ref No. required.")]
public int? EnquiryID { get; set; }
public ALResults alResultsModel { get; set; }
}
public class ALResults
{
public int ResultsID { get; set; }
public int InitialApplicantLevel2ID { get; set; }
public string Stream { get; set; }
public string Grading { get; set; }
public string IndexNo { get; set; }
public int? Year { get; set; }
public int? Attempt { get; set; }
public double? ZScore { get; set; }
public string Medium { get; set; }
}
#model SIMS.Models.StudentIntake.InitialApplicantLevel2Model
<tr>
<td>Year: </td>
<td>#Html.TextBoxFor(o=>o.alResultsModel.Year)</td>
<td>Index No: </td>
<td>#Html.TextBoxFor(o=>o.alResultsModel.IndexNo)</td>
<td>Medium: </td>
<td>#Html.TextBoxFor(o=>o.alResultsModel.Medium)</td>
</tr>
<tr>
<td>Stream: </td>
<td>#Html.TextBoxFor(o=>o.alResultsModel.Stream)</td>
<td>Attempt: </td>
<td>#Html.TextBoxFor(o=>o.alResultsModel.Attempt)</td>
<td>Zscore: </td>
<td>
#Html.TextBoxFor(o=>o.alResultsModel.ZScore)
</td>
</tr>
I found the answer here
Unfortunately Kendo UI doesn't support Class Composition / View Models
containing complex objects, your View Models need to be completely
flat to avoid unexpected behaviour.
I have a Model that named word.
this is my word model
public class Word : BaseFieldsTables
{
int ID { get; set; }
string Name { get; set; }
Guid UniqID { get; set; }
DateTime CreatedDate { get; set; }
byte[] RowVersion { set; get; }
public string Text { get; set; }
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
public virtual Language Language { get; set; }
public int LanguageID { get; set; }
public virtual ICollection<Picture> Pictures { get; set; }
[InverseProperty("MainWord")]
public virtual ICollection<RelationshipBetweenWords> MainWords { get; set; }
[InverseProperty("RelatedWord")]
public virtual ICollection<RelationshipBetweenWords> RelatedWords { get; set; }
}
word has a category and language and...
for example this is my language model
public class Language : BaseFieldsTables
{
int ID { get; set; }
string Name { get; set; }
Guid UniqID { get; set; }
DateTime CreatedDate { get; set; }
byte[] RowVersion { set; get; }
public virtual ICollection<Word> Words { get; set; }
}
i have word word viewmodel like this
public class WordViewModel
public string Name { get; set; }
public IList<SelectListItem> Categories { set; get; }
[HiddenInput(DisplayValue = false)]
public int SelectedCategoryID { set; get; }
public IList<SelectListItem> Languages { set; get; }
[HiddenInput(DisplayValue = false)]
public int SelectedLanguageID { set; get; }
}
and this my controller
public ActionResult Index(
{
IList<Word> list = _wordService.GetAll();
}
i want to get all word and map(auotomapper) to my WordViewModel with languagename and category name and return in my view please help me if i have to change my wordviewmodel or... and how to map.tnx
oh i found it
public ActionResult Index()
{
var orders = _respository.GetAll();
AutoMapper.Mapper.CreateMap<Order, OrderDto>()
.ForMember(dest => dest.OrderNumber, opt => opt.MapFrom(src => src.OrderNo))
.ForMember(dest => dest.OrderItemsDto, opt => opt.Ignore());
var model = AutoMapper.Mapper.Map<IEnumerable<Order>, IEnumerable<OrderDto>>(orders);
return View(model);
}
//Razor View Code
<h2>Orders</h2>
<table>
<tr>
<th>Order Number</th>
<th>Name</th>
<th>Total</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>#item.OrderNumber</td>
<td>#item.CustomerName</td>
<td>$#item.Total</td>
</tr>
}
</table>
I am new to asp.net and c#, I have been trying to duplicate and app done in php using MVC3, generating the code from the same database. I have a question about showing the detail records in my "Details" view.
namespace MvcApplication5.Models
{
[DataContract(IsReference = true)]
[KnownType(typeof(masterDomain))]
[KnownType(typeof(masterFIP))]
[KnownType(typeof(masterSFPF))]
[KnownType(typeof(rgCountiesServed))]
[KnownType(typeof(rgKeyword))]
[KnownType(typeof(rgServicesProvided))]
public partial class rgOrganization
{
public rgOrganization()
{
this.rgCountiesServeds = new HashSet<rgCountiesServed>();
this.rgKeywords = new HashSet<rgKeyword>();
this.rgServicesProvideds = new HashSet<rgServicesProvided>();
}
[DataMember]
public int orgID { get; set; }
[DataMember]
public string ORGANIZATION { get; set; }
[DataMember]
public string CONTACT_PERSON { get; set; }
[DataMember]
public string PHONE_NUMBER { get; set; }
[DataMember]
public string FAX_NUMBER { get; set; }
[DataMember]
public string EMAIL_ADDRESS { get; set; }
[DataMember]
public string WEBSITE { get; set; }
[DataMember]
public string STREET_1 { get; set; }
[DataMember]
public string STREET_2 { get; set; }
[DataMember]
public string CITY { get; set; }
[DataMember]
public string ZIP_CODE { get; set; }
[DataMember]
public string COUNTY { get; set; }
[DataMember]
public string FIPS { get; set; }
[DataMember]
public string MAILING_ADDRESS { get; set; }
[DataMember]
public Nullable<int> SFPF { get; set; }
[DataMember]
public Nullable<int> DOMAIN { get; set; }
[DataMember]
public virtual masterDomain masterDomain { get; set; }
[DataMember]
public virtual masterFIP masterFIP { get; set; }
[DataMember]
public virtual masterSFPF masterSFPF { get; set; }
[DataMember]
public virtual ICollection<rgCountiesServed> rgCountiesServeds { get; set; }
[DataMember]
public virtual ICollection<rgKeyword> rgKeywords { get; set; }
[DataMember]
public virtual ICollection<rgServicesProvided> rgServicesProvideds { get; set; }
}
View Code
#model MvcApplication5.Models.rgOrganization
#{
ViewBag.Title = #Html.DisplayFor(model => model.ORGANIZATION);
}
#section Scripts {
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false"></script>
}
<h2>#Html.DisplayFor(model => model.ORGANIZATION)</h2>
<h3><span id="mailing_address">
#Html.DisplayFor(model => model.MAILING_ADDRESS)
</span></h3>
<fieldset>
<legend> #Html.DisplayFor(model => model.COUNTY) COUNTY</legend>
<div class="display-field">
<strong>#Html.DisplayFor(model => model.ORGANIZATION)</strong>
</div>
<div class="display-field">
#Html.DisplayFor(model => model.CONTACT_PERSON)
</div>
<!-- would like to display services provided here -->
</fieldset>
My question is how do I access the 'rgCountiesServeds'and 'rgServicesProvideds' via Razor?
They appear to be members of your model.
So you can access the collection as
model.rgServicesProvided
To iterate through the collection, do something like this:
#foreach (var serv in model.ServicesProvided) {
Service is #serv.ToString()<br/>
}
Replace serv.ToString() with whatever property you want to display.
If I understand what you're asking, you can access the rgCountiedServeds and rgServicesProvideds just like the other properties of rgOrganization. They are collections and properties of your model so you can loop over them in the View or create a drop down list for each of those properties. I.e.,
#Html.DropDownList("DropDownCounties", Model.rgCountiesServeds)
i want to get the name of model genre and title of model list in the partial view but #genre.Lists.Title doesn't work
this is my genre model
public class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<List> Lists { get; set; }
}
and this is my List model
[Bind(Exclude = "ListId")]
public class List
{
[ScaffoldColumn(false)]
public int ListId { get; set; }
[DisplayName("Genre")]
public int GenreId { get; set; }
[DisplayName("Maker")]
public int MakerId { get; set; }
[Required(ErrorMessage = "An List Title is required")]
[StringLength(160)]
public string Title { get; set; }
[Required(ErrorMessage = "Price is required")]
[Range(0.01, 100.00,ErrorMessage = "Price must be between 0.01 and 100.00")]
public decimal Price { get; set; }
[DisplayName("List URL")]
[StringLength(1024)]
public string ListUrl { get; set; }
public Genre Genre { get; set; }
public Maker Maker { get; set; }
public virtual List<OrderDetail> OrderDetails { get; set; }
}
and this my actionResult
public ActionResult Navbar()
{
var genres = storeDB.Genres.Include("Lists").ToList();
return PartialView("Navbar",genres);
}
and this is my PartialView
#model IEnumerable<Store.Models.Genre>
#foreach (var genre in Model)
{
#genre.Name
#genre.Lists.Title
}
#genre.Lists is of type List<List>, not List (by the way, I would rename your class somehow, it's easy to confuse with the standard library class of this name).
So you either need another foreach loop to iterate over #genre.Lists or you can get the first element with #genre.Lists[0].Title. It's up to you what you actually want to achieve. For example, you could use string.Join:
#model IEnumerable<Store.Models.Genre>
#foreach (var genre in Model)
{
<text>
#genre.Name
#string.Join(", ", genre.Lists.Select(x => x.Title))
</text>
}
Or write some real HTML. Again, depends what you want your output to be.