Object' Does Not Contain a Definition for 'X'

object' does not contain a definition for 'X'

You are using an anonymous object here:

ViewBag.Languages = db.Languages
.Select(x => new { x.Name, x.EnglishName, x.Id })
.ToList();

Anonymous objects are emitted as internal by the compiler. The Razor views are automatically compiled into a separate assembly by the ASP.NET runtime. This means that you cannot access any anonymous objects generated in your controllers.

So in order to fix your issue you could define a view model:

public class LanguageViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string EnglishName { get; set; }
}

and then in your controller use this view model:

ViewBag.Languages = db.Languages
.Select(x => new LanguageViewModel
{
Name = x.Name,
EnglishName = x.EnglishName,
Id = x.Id
})
.ToList();

And now that you have a view model the next improvement to your code is of course to get rid of this crap of ViewBag that I am sick of seeing and simply use view models and strong typing:

public ActionResult Foo()
{
var model = db
.Languages
.Select(x => new LanguageViewModel
{
Name = x.Name,
EnglishName = x.EnglishName,
Id = x.Id
})
.ToList();
return View(model);
}

and then of course have a strongly typed view:

@model IEnumerable<LanguageViewModel>
@Html.DisplayForModel()

and then define the corresponding display template which will automatically be rendered by the ASP.NET MVC engine for each element of the view model so that you don't even need to write a single foreach in your views (~/Views/Shared/DisplayTemplates/LanguageViewModel.cshtml):

@model LanguageViewModel
... generate the image or whatever you was attempting to do in the first place

'object' does not contain a definition for 'Ten''

You cannot use an anonymous type to pass data from action method to view, via ViewBag.

Create a view model class to represent this data you want to pass.

public class YourVm
{
public string Ten { set; get; }
public string DuongDan { set; get; }
public string Icon { set; get; }
public string TrangThai { set; get; }
}

and in your LINQ expression, do the projection using this view model instead of anonymous type.

 new YourVm {  Ten = x.Ten, 
DuongDan = x.DuongDan,
Icon= x.Icon,
TrangThai = x.TrangThai })

Transform' does not contain a definition for 'activeSelf'

activeSelf is a property of GameObject, not Transform.

https://docs.unity3d.com/ScriptReference/GameObject-activeSelf.html

So, you'd do this:

    if(SpawnPointBlue.gameObject.activeSelf == true)
{
Instantiate(Mercury, SpawnPointBlue.position, SpawnPointBlue.rotation);
}

However, note that activeSelf just indicates if that object is active based on its own local setting. It could still be inactive in the scene if, for instance, its parent object is inactive. I'd recommend using activeInHierarchy instead.

https://docs.unity3d.com/ScriptReference/GameObject-activeInHierarchy.html

    if(SpawnPointBlue.gameObject.activeInHierarchy)
{
Instantiate(Mercury, SpawnPointBlue.position, SpawnPointBlue.rotation);
}

object does not contain a definition for 'Name'

You can do this in three ways, first one is by changing the return type of the GetBasic from object to GetData then the signature of the method will looks like the following:

public GetData GetBasic()
{
// do Something here
}

In this case you need not to change anything more, your code will works fine as you expected.

Second option for you is change the method call without changing the method definition, which will looks like the following:

GetData objects = (GetData)Data.GetBasic();

Which will Work since GetData having field called Name.

Third option is make GetBasic as a constructor like the following:

public object GetData()
{
string json = @"{'Name': 'Bad Boys','ReleaseDate': '1995-4-7T00:00:00'}";
GetData Data = JsonConvert.DeserializeObject<GetData>(json);
return Data;
}

So that you can use like this:

 GetData DataObjects = new GetData();
// DataObjects.Name will have the expected value


Related Topics



Leave a reply



Submit