Stephen
Stephen

Reputation: 803

.NET MVC relationships

I am trying to wrap my head around relationships and and I hoping someone can clarify this for me.

I have the following two models

public class dsExpressionOfInterest
{
    public int dsExpressionOfInterestID { get; set; }

    [Display(Name = "Customer Name")]
    public string CustomerName { get; set; }

    public virtual dsRegistration dsRegistration { get; set; }
}

and

public class dsRegistration
{
    public int dsRegistrationID { get; set; }

    [Display(Name = "Expression Of Interest ID")]
    public int dsExpressionOfInterestID { get; set; }

    [Display(Name = "SLA Received")]
    public bool SLAReceived { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Date Received")]
    public DateTime? SLAReceivedDate { get; set; }
}

On the Index View of dsRegistration I would like to be able to display the CustomerName field from dsExpressionOfInterest, however this is not available to me.

How should my navigational properties be set in order to facilitate this?

UPDATE

My Controller

public ActionResult Index()
{
    var dsregistration = db.dsRegistration.Include(d => d.Employee).Where(d => d.PackSent == false);

    return View(dsregistration);
}

There are other additional fields which I have not shown in the above models in order to simplify the problem.

Upvotes: 0

Views: 250

Answers (2)

sdm350
sdm350

Reputation: 397

Assuming that this is a one-to-one relationship, it is setup wrong.

I'm assuming that each Registration has exactly one ExpressionOfInterest, and each ExpressionOfInterest has exactly one Registration.

In order to have one-to-one relationships in MVC4, the primary key of the child must ALSO be the foreign key. The relationship should look like this:

public class dsRegistration
{
    public int dsRegistrationID { get; set; }

    [Display(Name = "SLA Received")]
    public bool SLAReceived { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Date Received")]
    public DateTime? SLAReceivedDate { get; set; }

    //Indicate that a dsRegistration has an expresison of interest
    public dsExpressionOfInterest expressionOfInterest { get; set;}
}

and

public class dsExpressionOfInterest
{
    //Foreign key
    [Key, ForeignKey("dsRegistration")]
    public int dsExpressionOfInterestID { get; set; }

    [Display(Name = "Customer Name")]
    public string CustomerName { get; set; }

    public virtual dsRegistration dsRegistration { get; set; }
}

and now in your controller you should be able to use

var dsregistration = db.dsRegistration.Include("dsExpressionOfInterest").Where(d => d.PackSent == false);

finally, in the view you should be able to access registration.expressionOfInterest.CustomerName

Upvotes: 1

Katie Kilian
Katie Kilian

Reputation: 6985

Make a ViewModel that is different from your usual domain model. The ViewModel should contain the fields needed for the view. You probably will have a different ViewModel for each action. For example, if the action is named Index:

public class RegistrationIndexViewModel
{
    public int dsRegistrationID { get; set; }

    [Display(Name = "Expression Of Interest ID")]
    public int dsExpressionOfInterestID { get; set; }

    [Display(Name = "SLA Received")]
    public bool SLAReceived { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Date Received")]
    public DateTime? SLAReceivedDate { get; set; }

    [Display(Name = "Customer Name")]
    public string CustomerName { get; set; }
}

You pass the view model to the dsRegistration view instead of the domain model. The domain models work with the database and enforce the business logic. The view models are passed to the views.

Upvotes: 1

Related Questions