Asp.net MVC2: I have many dropdownlists in my mvc application. At first, I started by creating a table for each one with a unique ID and name and referring to them in the controllers and views. The application got bigger, and it was suggested that I use a lookup table, that contains lookuptype and lookupvalue as compound primary key and fill all the values for the dropdownlists in it. I've looked all over the internet, the only method used for mvc is one table for each dropdownlist! Can someone explain to me how I can implement it, and in detail please becoz I'm totally lost. A link to a tutorial would also be great.
I'm using vb.net and linq to sql.
Suppose your tables have columns ID, Name and Value. Now by having only one table that table would most probably look like this:
create table Lookup
(
LookupID int not null identity
primary key,
LookupTypeID int not null
references LookupType(LookupTypeID),
Name nvarchar(50) not null,
Value int not null,
unique(EnumTypeID, Name)
)
go
This table will make sure that within the same type names don't clash.
Anyway. You could of course have a similar application (not data) model class
public class EnumValue
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Value { get; set; }
}
So whenever you get values of a certain type you can always generate an IList<EnumValue> of them and feed them to a particular drop down.
Since you didn't provide any valuable data this table and class could omit column/property Value, because depending on the inner workings it may not be needed at all. But you will know this best because you know your application requirements. Instead of this column/property only ID could do the trick.
I actually used a slightly different approach. I created a table that has a composite primary key, LookupName and LookupValue. Then in the datacontext I declared a method that takes the lookupname as a parameter, and then brings a list of lookupvalues whose lookupname match this parameter. In the original table (ex.Contact) I created a field called status, where the selected value will be saved. Then in the controller, I used viewdata to create dropdownlists.
Example: _db represents the datacontext
Viewdata('Status')= new selectlist(_db.Getlookupname('status'),'lookupname','lookupname')
and then in the view
html.dropdownlist('status')
I also named the dropdownlist with the same fieldname 'status' that is found in the target table 'Contact'.
And it worked, without any complexity or errors.
Thanks for the help. And I hope this will be helpful to someone else!
Here's a sort of tutorial on how to achieve a generic lookup service.
http://wtfperminute.blogspot.com/2011/02/working-with-reference-data-lookups.html
Similar to what you've done, but perhaps a bit more comprehensive.
Related
I am new to ASP.NET MVC, especially Entity Framework.
I am trying to save JSON data from external service to SQL server. I use Code First and it creates tables and inserts all records. But original ID values are changed to SQL auto generated ID value. For example, original ID 20050 become 1. Please check attached photos. I googled my problem and did not find a solution.
This is my fist time posting a question in stackoverflow too. Thanks in Advance.
you can set identity specification to false to that table usine DataAnnotations attribute.
[DatabaseGenerated(DatabaseGeneratedOption.None)];
public int ID { get; set; }
Than apply migration.
VS2013 update 4, VB (or C#), EF 6.0, SQL, LINQ
This is a difficult question for me to ask in a clear manner because I'm uncertain of the proper terms to use. The sum of it is my code deletes items in a record, but sets the pointer in the secondary table to NULL instead of deleting the record there. The following is a simple representation of my model and code.
Public Class DbContext
Public Property records As DbSet(Of record)
End Class
Public Class record
Public Property recordID As Integer
Public Property DataItems As New List(Of DataItem)
End Class
Public Class DataItem
Public Property DataItemID As Integer
Public Property Data As Integer
End Class
If I execute code using a .Remove() each record in records will end up with 0 DataItems (I can see that in VS debug mode) but the DataItem table is still full of records, all with NULL pointers. (This paragraph changed after initial post & incorrect code that was referred to in a comment was removed)
I understand that I could add the following to the Public Class DbContext:
Public Property DataItems As DbSet(Of DataItem)
Then I could go through and remove all DataItems that have the NULL pointer, but this doesn't seem like the way it's supposed to be done, so I'm asking for help to understand how I should be coding in this situation.
Is there a proper way in LINQ to remove a record from a List such that the actual record is also removed instead of merely having the pointer set to NULL?
To provide an answer for others who have the same question, the comments above provide the solution that worked for me. I had to manually delete the children, and in other cases create a method to search for and delete records with NULL in the return pointer. The suggestion to use RemoveRange() worked well. While setting up the cascade delete seems a good solution, I didn't want to manually set that up in SSMS, so I do the manual deletions for now until someone can explain how to set this up in EF from the model definition.
I'm using ASP.NET dynamic data to create a ticket edit form. The Ticket is associated to a problem code, but not using the problem code's primary key. (The database is 3rd party, so I don't have control over that...)
So the problemCode looks basically like:
ProblemCode
(
ID int,
Code varchar(8),
Description varchar(250)
)
The association is between Ticket.ProblemCode and ProblemCode.Code
Now the problem is that ForeignKey_Edit (really, PopulateListControl) fills the dropdown with the Primary Key which is ID... but the ticket is actually using Code to tie the two together. Am I missing something? Is there a simple way to get it to use Code as the value instead?
If not I can probably eliminate ID from the view as I don't think it HAS to be used anywhere... I just like having an ID around.
Try this in your metadata:
[DisplayColumn("Code")]
public partial class ProblemCode{ }
I have a table that normally, upon insert, the auto-key will increment. But, there are some instances when we want to set the ID (as would easily be done with "IDENTITY INSERT" in SQL).
Is there a way to accomplish this with LINQ to SQL?
Thanks,
Take a look here: http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/566e9540-911e-48b4-ac31-f69c0ab9f7fb/
Last reply here: http://forums.asp.net/t/1208607.aspx
If you want to make IDENTITY increment always OFF
Edit the Model
public class TableName
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
...
}
While trying to use LINQ to SQL I encountered several problems.
I have table persons:
int ID
string firstName
string lastName
And table notes that has:
int ID
string noteText
string createdBy
datetime creationDate
int PersonID
PersonID is a foreign key and the relationship is 1:n
I tried to use LINQ to SQL to create a person and some notes for each person.
Person person = new person();
Person.firstName = "me";
Person.note = new note();
Person.note.noteText = "some text…";
_DataContext.Persons.InsertOnSubmit(person);
_DataContext.SubmitChanges();
The problem is that the person object doesn't yet exist in the DB so it doesn't have an ID yet. So the note.personID filed has a 0 value… (the ID field is an identity field in the sql server)
The only solution for this that I found is to create a person , submitchanges and then create a note and submitchanges again.
Am I missing something here or maybe that’s the way one should work with LINQ to SQL?
How can I add multiple notes per person with LTS? I have a 1:n relationship and I don't see it with LTS.
If a person has 10000 notes, I don't want the person object constructor to load all the notes he has. I want to load them only when I refer to them. How can I config LTS to load the notes on demand?
If you aren't using the LinqToSql class designer, you should think about using it. The classes it generates will support the insertion scenario you outlined.
I can tell you aren't using the designer because it would give you a Notes (plural) property on the Person... as Person is 1 to Many with Notes.
How can I config LTS to load the notes on demand?
The designer will generate a property of type EntitySet(Note), which will load Notes on demand.