I have a problem where my model MP
class 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>
You never actually do anything with this value:
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:
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:
That would set the
MP
property on theratings
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.