I have a controller for select stored procedure. Now I am trying to add view for this controller using template.
Controller code block
public ActionResult Display()
{
return View(db.P_GET_USER().ToList());
}
This is the code block of context class for the stored procedure:
public virtual ObjectResult<P_GET_USER_Result> P_GET_USER()
{
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<P_GET_USER_Result>("P_GET_USER");
}
Code block of P_GET_USER_Result.cs complex type:
public partial class P_GET_USER_Result
{
public string USERNAME { get; set; }
public string NAME { get; set; }
public int ROLE { get; set; }
public int STATUS { get; set; }
public string CREATED_DATE { get; set; }
}
When I try to create view keeping template as list and modal class as P_GET_USER_Result (final.Models), I get a pop up error
Unable to retrieve meta data for P_GET_USER_Result. One or more validation errors were detected during model generation
Please see the attached screenshot for the detailed error message.
When I searched the internet for tutorials, I found most of them show are creating empty template and then adding content in view. So can't I use list template. Please help.
Following your error, your Entity must have a key. You should change your Models:
public partial class P_GET_USER_Result
{
[key]
public int Id {get; set;}
public string USERNAME { get; set; }
public string NAME { get; set; }
public int ROLE { get; set; }
public int STATUS { get; set; }
public string CREATED_DATE { get; set; }
}
You can choose any fields set to key, but it's must identify.
Related
I have two Models, PurchaseOrders and PurchaseOrderMessages as shown below. I am implementing this in such a way that a user, while creating a purchase order, enters the purchase order details, PurchaseOrderName and a PurchaseOrderMessage. I am using my PurchaseOrderVM for the View. On submitting the details, in the POST method, like this:
_context.Add(purchaseOrdersObj);
_context.Add(purchaseOrdersMessagesObj);
_context.SaveChanges();
I am getting the following error: The INSERT statement conflicted with the FOREIGN KEY constraint. I understand why I am getting that error. It's becuase my FOREIGN KEY, PurchaseOrderId is not present in the database yet, as the data for PurchaseOrders has not been saved.
Is there a way in which I can save the Message to the PurchaseOrderMessages the same time I save details for PurchaseOrders? Also, it has to be done in such a way that the user should be able to add more PurchaseOrderMessages in the future to the same PurchaseOrder, once the PurchaseOrder has been created. And, all the PurchaseOrderMessages should be saved so that they can all be printed on the screen one after another.
PurchaseOrders.cs
public class PuchaseOrders
{
[Key]
public int PurchaseOrderId { get; set; }
public string PurchaseOrderName { get; set; }
}
PurchaseOrderMessages.cs
public class PurhcaseOrderMessages
{
[Key]
public int UpdateId { get; set; }
public string Message { get; set; }
[ForeignKey("PurchaseOrder")]
public int PurchaseOrderId { get; set; }
public virtual PurchaseOrder PurchaseOrder { get; set; }
}
PurchaseOrderVM.cs
public class PurchaseOrderVM
{
public int PurchaseOrderId { get; set; }
public string PurchaseOrderName { get; set; }
public string Message { get; set; }
}
You need to allow EF to wire this up, so it can handle saving the objects in the right order. Instead of setting an id, set the navigation property:
purchaseOrdersMessagesObj.PurchaseOrder = purchaseOrdersObj;
_context.Add(purchaseOrdersMessageObj);
_context.SaveChanges();
You can do this by two solutions:
First:
public class PuchaseOrders
{
public PuchaseOrders(){
Messages = new List();
}
[Key]
public int PurchaseOrderId { get; set; }
public string PurchaseOrderName { get; set; }
public ICollection<PurhcaseOrderMessages> Messages{ get; set; }
}
PuchaseOrders purchaseOrdersObj= new PuchaseOrders(){ PurchaseOrderName = "Pla Pla"};
purchaseOrdersObj.Messages.Add(new PurhcaseOrderMessages(){Message = "Pla Pla"});
_context.Add(purchaseOrdersObj);
_context.SaveChanges();
Second: if you work with SP (stored procedures) in this project a lot you can write SP to take the two objects data and insert them
I'm having trouble understanding how to implement a ViewModel in Asp.net MVC, I have the following tables:
Form
ID, Data
Report
ID, FormID, Owner, Category, Status, SubmissionDate
ReportValues
ID, ReportID, Title, Value
I'm looking for a way to display and edit Report and ReportValues in the one ViewModel where ReportValues.ReportID = Report.ID
ReportValues will have multiple entries that relate to a Report.
I have had a look at similiar questions on here and tried following a tutorial ( http://techfunda.com/howto/262/list-data-using-viewmodel ) and coming up empty handed.
If you need any more information let me know and thanks in advance for any replies!
Your View Model is nothing more than a class. You can solve this many ways, but here's an example.
Create your 3 classes like you normally would.
public class Form
{
public int Id { get; set; }
public string Data { get; set; }
}
public class ReportValues
{
public int Id { get; set; }
public int ReportId { get; set; }
public string Title { get; set; }
public string Value { get; set; }
}
public class Report
{
public int Id { get; set; }
public int FormId { get; set; }
public string Owner { get; set; }
public string Category { get; set; }
public string Status { get; set; }
public DateTime SubmissionDate { get; set; }
}
Then, create your ViewModel class to include the three above classes like this.
public class ReportViewModel
{
public Form Form { get; set; }
public ReportValues ReportValues { get; set; }
public Report Report { get; set; }
}
In your view you can access your three classes and their properties as you would in your controller. Model.Form.Id
Depending on your data types, ReportValues will likely be a property of Report, but that's entirely up to your data structure. You will need to populate the classes using whatever method you want (Entity Framework, ADO, etc.) before you can pass them to your view and use them.
I'm making a simple website where I'm storing some information that I get from an excel file into my models class and retrieving them from the html page. The following class is a class in my models:
public class ToxinInformation
{
public string cas_rn { get; set; }
public string critical_effect { get; set; }
public string point_of_departure { get; set; }
public string adi_tdi { get; set; }
public string intake { get; set; }
public string hazard_quotient { get; set; }
public string comment { get; set; }
public string tox_link { get; set; }
public string tox_link_decription { get; set; }
public string intake_link { get; set; }
public string intake_link_description { get; set; }
public IList<string> Links { get; set; }
}
And I use this code to set the information in my controller class and return the view:
(of course I would set information all the variables, not only the first one)
var model = new ToxinInformation
{
cas_rn = "lol"
};
return View(model);
So far I can easily set all my strings and my list and retrieve them on my html page, but what do I do if in some cases I need several instances of the class "ToxinInformation"? In some cases I have 2 or more set of data I'd like to save and show in HTML except for just one.
Any suggestions would be very helpful.
You should make a list and add instances of model to the list. Then you can use a DisplayFor or EditorFor template to show them all.
var models = new List<ToxinInformation>();
foreach(var dataBlob in YourDataStore)
{
var model = new ToxinInformation()
{
cas_rn = dataBlob.cas_rn // Not sure where your raw data is coming from.
}
models.add(model)
}
return View(models);
I'm using VS 2010 with Razor/MVC 3 to create a form where User 1 submits information to a database using RequestQueue Model (this is just a queue table). User 2 then pulls this information, approves it, and the data is sent to the main database via DbRequestForm Model. This model, however, consists of several sub-models (Data_Dictionary, SERVER_DATABASE, and DatabaseRequestInfo) because the submitted data needs to go to three separate tables.
The problems is, whenever I approve the data for submission from RequestQueue to DbRequestForm, I get an error saying there is no primary key for DbRequestForm. However, there is a PK for each sub-model. I have also tried adding [Key] public int SrvrDB_ID { get; set; } and other variations to DbRequestForm to no avail.
Initial Model:
public class RequestQueue
{
[Key]
public int SrvrDB_ID { get; set; }
public string dbName { get; set; }
public int InitialSpaceNeeded { get; set; }
public string ScheduledJobSetup { get; set; }
public string PII_data { get; set; }
}
Encapsulating Model:
public class DbRequestForm
{
public Data_Dictionary Data_Dictionary { get; set; }
public DatabaseRequestInfo DatabaseRequestInfo { get; set; }
public SERVER_DATABASE SERVER_DATABASE { get; set; }
}
Models based on final three tables:
public class Data_Dictionary
{
[Key]
public int SrvrDB_ID { get; set; }
public string PII_data { get; set; }
}
public class SERVER_DATABASE
{
[Key]
public int SrvrDB_ID { get; set; }
public string dbName { get; set; }
}
public class DatabaseRequestInfo
{
[Key]
public int SrvrDB_ID { get; set; }
public int InitialSpaceNeeded { get; set; }
public string ScheduledJobSetup { get; set; }
}
Any ideas how to avoid this error? Thanks in advance!
EDIT: The specific error is '
System.Data.Edm.EdmEntityType: : EntityType 'DbRequestForm' has no key
defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet DbRequestForms is
based on type DbRequestForm that has no keys '
The error tells that you need to define key for DBRequestForm.
Code First would infer that a property is a primary key if the property is called ‘Id’ or ‘class name Id’.
In your case you have to add
[Key]
public int DBRequestFormId { get; set; }
This concrete error won't appear anymore.
here is detailed information how code first works
http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx
see the Primary Key section
I am a MVC newbie so go easy on me please.
I am getting two errors when I try to add a migration. They are as follows:
EntityType 'Icon' has no key defined. Define the key for this EntityType.
EntityType: EntitySet 'Icons' is based on type 'Icon' that has no keys defined.
I am including the Icon inside another model, like so:
public class Icon
{
public string IconName { get; set; }
public string IconColor { get; set; }
public int BackgroundXPos { get; set; }
public int BackgroundYPos { get; set; }
public string IconColorHover { get; set; }
public int BackgroundHoverXPos { get; set; }
public int BackgroundHoverYPos { get; set; }
}
public class GalleryThumbnail : CSSBoxModel
{
[DisplayName("Thumbnail Image Outline Color")]
public string ThumbnailImageOutlineColor { get; set; }
[DisplayName("Thumbnail Menu Font")]
public CSSFont ThumbnailMenuFont { get; set; }
[DisplayName("Thumbnail Icon Color")]
public Icon ThumbnailIconColor { get; set; }
}
How is this Address class below any different which is working:
public class Address
{
public String Adress1 { get; set; }
public String Adress2 { get; set; }
public String Adress3 { get; set; }
public String City { get; set; }
public String County { get; set; }
public String State { get; set; }
public String Zip { get; set; }
public String Country { get; set; }
}
[Table("UserProfile")] //Could be PP empolyee, Subscriber or Subscriber's customer
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public bool? Gender { get; set; }
public Address Address { get; set; } //billing address
public Address ShipAddress { get; set; }
}
I did not add a key in either my Icon or Address class because I have no intention of storing specific data in my DB. They are merely to be used inside other classes. So wy is one neededing an ID and the other is not?
I have not created public DbSet Icons { get; set; } in my DB Context either.
Also can you tell me what it is called when you use a class inside another ( or instance of class inside a class as in these examples ) ?
Much appreciated!
Since the address entity has no key defined it the Entity Framework assumes it's a complex property, and your UserProfile table will be rendered with columns named Addres_Address1, Address_Address2, Address_Address3, Address_City, and so on...
Even though you haven't declared an EntitySetIcons DbSet on your context class, it's still being added implicitly because one of your other classes somewhere has an ICollection or IEnumerable property defined.
More info on Code Conventions here:
http://msdn.microsoft.com/en-us/data/jj679962.aspx
So, either decorate the collections as NotMapped like #Kamyar said or simply remove the references from any class already declared as a DbSet.
you can use [NotMapped] attribute in System.ComponentModel.DataAnnotations.Schema namespace in EntityFramework.dll:
using System.ComponentModel.DataAnnotations.Schema;
...
[NotMapped]
public Address Address { get; set; } //billing address
[NotMapped]
public Address ShipAddress { get; set; }
Regarding the naming, AFAIK these are called public properties as well.