Share via


Viewmodel does not contain a defintion for and no extension method accepting a first argument of type

Question

Saturday, February 7, 2015 8:25 AM

I have struggled with this now since i sarted using viewmodels. I was told that creating viewmodels would make it easier, but in my case it's a pain. I really hope that I am doing something wrong here so i can start understand this.

So, after creating my viewmodel i get this error in my view:

MvcTest1.Viewmodel.ResultatViewModel does not contain a defintion for "Plassering"and no extension method "Plassering" accepting a first argument of type MvcTest1.Viewmodel.ResultatViewModel could be found. Are you missing.... etc.

Resultatviewmodel:

using MvcTest1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcTest1.ViewModels
{
    public class ResultatViewModel
    {
        public IEnumerable<Resultat> Resultater { get; set; }
        public virtual Stevne Stevne { get; set; }
    }
}

Resultat model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcTest1.Models
{

    public class Resultat
    {
        public int ResultatId { get; set; }
        public int Plassering { get; set; }

        public int StevneId { get; set; }
        public virtual Stevne StevneNavn { get; set; }

    }
}

Stevneresultater.cshtml view (works just fine):

@model MvcTest1.ViewModels.ResultatViewModel

<h4>Resultater for @Model.Stevne.StevneNavn</h4>

<p>
    @Html.ActionLink("Create New", "Create", new { stevneId = Model.Stevne.StevneId })
</p>

@Html.Partial("_Resultater", @Model.Stevne.StevneResultat)

_Resultater.cshtml (Partial view, here is the error):

@model MvcTest1.ViewModels.ResultatViewModel

<table class="table">

    <tr>

        <th>
            @Html.DisplayNameFor(model => model.Plassering) <-- Error on this!!!!
        </th>
    </tr>

    @foreach (var item in Model.Resultater)
    {
        <tr>

            <td>
                @Html.DisplayFor(modelItem => item.Plassering) <-- No error here!!!
            </td>
            <td>
                @Html.ActionLink("Endre", "Edit", new { id = item.ResultatId })

            </td>
        </tr>
    }

</table>

Resultat actionresult:

// GET: Resultat for stevne
        public ActionResult Stevneresultat([Bind(Prefix="id")] int stevneId, string gruppeNavn)
        {

            var resultater = db.Resultater.Include(r => r.KasterNavn);

            var stevner = db.Stevner.Find(stevneId);
            if (stevner != null)
            {
                return View(new ResultatViewModel
                {
                    Resultater = resultater,
                    Stevne = stevner
                });
            }

            return HttpNotFound();
        }

All replies (7)

Sunday, February 8, 2015 7:56 PM âś…Answered

Since you are passing back a list of Plassering, you need to specify the element

@Html.DisplayNameFor(model => model.Resultater.First().Plassering)

This was discussed in this thread for the reference

http://stackoverflow.com/questions/16692794/how-do-you-access-the-displaynamefor-in-a-nested-model


Saturday, February 7, 2015 8:40 AM

Hmmm, have you cleaned, rebuilt your project?


Saturday, February 7, 2015 8:47 AM

From what I can your passing a ienumerable collection of <br>
Resultater and you won't be able to reference the particular field how your currently doing as it's a list you will need to loop through to obtain such variable (which is pointless and time consuming) considering you only need the one field,
<br>
<br>
try using linq to filter out the field you need. something like this<br>
<br>
@{
var t = (from x in model select x.plassering)
}

put the above in the view and put a breakpoint on it and see what the value of t is


Saturday, February 7, 2015 10:23 AM

First post: Rebuilding and cleaning didnt solve it.

Ok, so I removed all code from _Resultat view and added this:

@{
var t = (from x in Model.Resultater select x.Plassering);
}

Now i get different error: 

The model item passed into the dictionary is of type 'System.Collections.Generic.HashSet`1[MvcTest1.Models.Resultat]', but this dictionary requires a model item of type 'MvcTest1.ViewModels.ResultatViewModel'.

Line 11: @Html.Partial("_Resultater", @Model.Stevne.StevneResultat)

Im wondering if the query on my controller is wrong:

public ActionResult Stevneresultat([Bind(Prefix="id")] int stevneId, string gruppeNavn)
        {

            var resultater = db.Resultater.Include(r => r.KasterNavn);

            var stevner = db.Stevner.Find(stevneId);
            if (stevner != null)
            {
                return View(new ResultatViewModel
                {
                    Resultater = resultater,
                    Stevne = stevner
                });
            }

* *


Sunday, February 8, 2015 2:30 PM

Can I see the definition for the class 'Stevne'?


Sunday, February 8, 2015 7:25 PM

<th> @Html.DisplayNameFor(model => model.Plassering) <-- Error on this!!!! </th>

Should be:

@Html.DisplayNameFor(model => model.Resultater.Plassering)

since in your viewmodel, your Resultater property is the one containing the Plassering property


Monday, February 9, 2015 2:54 AM

Hi slarur,

Base on your code, you have the collection of Resultat object and the property of Plassering is in Resultat class.

So, you need to get specify Resultat object, then access Plassering property.

@Foreach(var r in Model.Resultater)
{
  <div>
   @r.Plassering
  </div>
}

I suggest that you could change IEmumerable to List, then you could retrieve specify object through index and this is better for model binding.

@Html.DisplayNameFor(model => model.Resultater[0].Plassering)

Regards

Starain