Brian
Brian

Reputation: 427

Cannot convert from int to ApiModels.Enums.EnumStuff

The two errors that it is giving are,cannot convert from in to ApiModels.Enums.ContentAreaEnum and the best overloaded method match for '' has some invalid arguments I am trying to display a simple list from the database using a repository, API model, and a service.I do not have much experience working with either so I would like to get a full understanding of the errors.

In my index I am attempting to display the content, but I am getting the errors.

public ActionResult Index(int id)
{
    ViewData["Claims"] = _ctsService.GetClaimsForContentArea(id);
    return View();    
}

This is the IService I am referencing:

List<Claim> GetClaimsForContentArea(ContentAreaEnum contentArea);

The Service:

public List<Claim> GetClaimsForContentArea(ContentAreaEnum contentArea)
{
    return _claimRepository.GetClaimsInContentArea((int)contentArea);
}

The Repository:

public List<Claim> GetClaimsInContentArea(int contentAreaId)
{
    var query = from c in _db.Claims
            where c.ContentArea_ID == contentAreaId
            select c;

    return query.ToList();
}

The IRepository:

List<Claim> GetClaimsInContentArea(int contentAreaId);

And the ApiModel:

public enum ContentAreaEnum
{
    Subject1 = 1,
    Subject2 = 2
}

Upvotes: 0

Views: 226

Answers (1)

kamil-mrzyglod
kamil-mrzyglod

Reputation: 5008

I believe you are refering to compilation errors.

Since your method signature looks like List<Claim> GetClaimsForContentArea(ContentAreaEnum contentArea); and you are trying to pass int to it, it won't work.

You have to either transform your int to enum or cast enum to int like:

public ActionResult Index(int id)
{

    ViewData["Claims"] = _ctsService.GetClaimsForContentArea((ContentAreaEnum)id);
    return View();

}

but then you don't need another transformation to int. I think you should change your GetClaimsForContentArea() method signature to GetClaimsForContentArea(int id) and cast this parameter to enum right there or get rid of whole transformation which doesn't seem necessary.

EDIT:

If you really need validation of int id value, you can add following code:

public ActionResult Index(int id)
{

    if(Enum.IsDefined(typeof(ContentAreaEnum), id)) {
       ViewData["Claims"] = _ctsService.GetClaimsForContentArea((ContentAreaEnum)id);
    }

    return View();

}

Upvotes: 1

Related Questions