none
Need to assign via ID, not entity RRS feed

  • Question

  • I have an entity that has a virtual poperty that I need to set.  When I post back to my mvc controller, the value the  user set comes from a dropdownlist.  I am populating my viewmodel with the ID of the seleted Location.  I then want to populate my model with model.LocationID = ViewModel.LocationID.  The problem is the LocationID is not available to me publically.  There is a field in the DB called Location_ID but it is not public.  Here is my model.

    public class Listing
    {
    	public int ID { get; set; }
    	public string Number { get; set; }
    	public string Description { get; set; }
    	public string URL { get; set; }
    	public DateTime LeaseDate { get; set; }
    	public int LeaseMonths { get; set; }
    
    	public virtual WebAccount Owner { get; set; }
    	public virtual Location Location { get; set; }
    
    }
    
    

     


    Thanks, Terrence
    Tuesday, October 18, 2011 3:10 AM

Answers

  • Hi Terrence,

    To tell you truth, I'm not very familiar with MVC, I think you can use LocationID to retrieve it from DbContext and then compare it from ViewModel.Location. In other way you can add the LocationID in your model and use Fluent API to config the mapping:

    modelBuilder.Entity<Product>() 
        .HasRequired(p => p.PrimaryCategory) 
        .WithMany(c => c.Products) 
        .HasForeignKey(p => p.PrimaryCategoryCode);
    

    have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 19, 2011 12:40 PM
    Moderator

All replies

  • Hi

    Is Listing entity is suppose to have LocationId property? 

    Did you added LocationID later to datatable but not update the entity model? what approach you are using to generate model?


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Tuesday, October 18, 2011 10:04 AM
  • Kris,

    Thank you for your help.

    I am using Code First.

    >>Is Listing entity is suppose to have LocationId property?

    Well because it has a virtual Location property, it must have a locationID to poinit at the row in the Location table.

    I have not added LocationID, I figured because I have a virtual Location property I should be able to get at the LocationID field that is in the table.

    Thank you for your help.


    Thanks, Terrence
    Tuesday, October 18, 2011 6:30 PM
  • Hi Terrence,

    To tell you truth, I'm not very familiar with MVC, I think you can use LocationID to retrieve it from DbContext and then compare it from ViewModel.Location. In other way you can add the LocationID in your model and use Fluent API to config the mapping:

    modelBuilder.Entity<Product>() 
        .HasRequired(p => p.PrimaryCategory) 
        .WithMany(c => c.Products) 
        .HasForeignKey(p => p.PrimaryCategoryCode);
    

    have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 19, 2011 12:40 PM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 24, 2011 1:53 AM
    Moderator