Model Property not displaying in Details page ASP.NET MVC 5

2.5k views Asked by At

I have a problem where my model MPclass property of CompanyName will not display within my Ratings details page of my ASP.NET MVC 5 project.

I am using two models for this view. The MP class is a foreign key to the Ratings class.

Model: Ratings

 public class Ratings
{
    //Rating Id (PK)
    public int Id { get; set; }

    [DisplayName("User Name")]            
    public string UserId { get; set; }

                 //Medical Practice (FK)
                 public int MpId { get; set; }
                 public MP MP { get; set; }

    //User ratings (non-key values)

    [DisplayName("Wait Time")]
    [Required] //Adding Validation Rule
    public int WaitTime { get; set; }

    [Required] //Adding Validation Rule
    public int Attentive { get; set; }

    [Required] //Adding Validation Rule
    public int Outcome { get; set; }


}

Controller: RatingsController

        // GET: Ratings/Details/5
    public ActionResult Details(int? id)
    {

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Ratings ratings = db.Ratings.Find(id);
        if (ratings == null)
        {
            return HttpNotFound();
        }

       MP CompanyName = db.MedicalPractice.Find(id);

        return View(ratings);
    }

Ratings View: Details

    <dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.MP.CompanyName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.MP.CompanyName)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.UserId)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.UserId)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.WaitTime)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.WaitTime)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Attentive)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Attentive)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Outcome)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Outcome)
    </dd>

</dl>
2

There are 2 answers

1
David On

You never actually do anything with this value:

MP CompanyName = db.MedicalPractice.Find(id);

There's no code which access it, and as soon as the method exists the value is gone. When you try to access the value in the view:

@Html.DisplayFor(model => model.MP.CompanyName)

You're trying to access a property on the model, but you never set that property on the model.

Presumably, based on the code/description, what you meant to do was this:

ratings.MP = db.MedicalPractice.Find(id);

That would set the MP property on the ratings object (which is your model), and allow you to access that property in the view.

The details may be slightly different in your case, since your object structures and naming conventions are a bit unclear. But the point is that you need to set the properties on the model in order to read them from the model. The model isn't going to know about other variables you created in your controller method.

0
Chih-Ho Andy Chou On

Use .Include to simply your Details method:

// GET: Ratings/Details/5
public ActionResult Details(int? id)
{

    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Ratings ratings = db.Ratings.Find(id).Include("MP");
    if (ratings == null)
    {
        return HttpNotFound();
    }       

    return View(ratings);
}