简体   繁体   中英

c# cannot implicitly convert type

I want to return the referencesModel object but I am getting a error about the type of object. Is there another way to do this?

public ActionResult Edit(string index)
{
    var selectedReference = template.References.First(reference => reference.SortOrder == index);
    ReferencesModel referencesModel = selectedReference;
    return PartialView(referencesModel);
}

Error:
(local variable) Data.Reference selectedReference
Cannot implicitly convert type 'OrderTemplateTool.Data.Reference' to 'OrderTemplateTool.Web.Models.References.RederencesModel"

Model:

public class ReferencesModel
{
    public int id { get; set; }
    public string Link { get; set; }
    public string Text { get; set; }
    public string Type { get; set; }
    public string Regimens { get; set; }
    public Guid GuidlineId { get; set; }
    public int SortOrder { get; set; }
}

You can add some constructers to your ReferenceModel class.

public class ReferencesModel
{
    public ReferencesModel()
    {

    }

    public ReferencesModel(Reference reference)
    {
        this.Id = reference.Id;
        this.Link = reference.Link;
        this.Text = reference.Text;
        this.Type = reference.Type;
        this.Regimens = reference.Regimens;
        this.GuidlineId = reference.GuidlineId;
        this.SortOrder = reference.SortOrder;
    }


    public int Id { get; set; }
    public string Link { get; set; }
    public string Text { get; set; }
    public string Type { get; set; }
    public string Regimens { get; set; }
    public Guid GuidlineId { get; set; }
    public int SortOrder { get; set; }
}

And use it like this

    public ActionResult Edit(string index)
    {
        var selectedReference = template.References.First(reference => reference.SortOrder == index);
        ReferencesModel referencesModel = new ReferencesModel(selectedReference);
        return PartialView(referencesModel );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM