I am having some issues that i cannot seem to figure out. What I am attempting to do is produce a list of records with a checkbox to select the record. The first issue i had was adding the checkbox to the model. It was telling me that it was an invalid column. So I created a view model with the added checkbox.
I get this error:
Cannot implicitly convert type 'System.Collections.GenericList' to System.Collections.Generic.List'
Here is the Controller GET:
public ActionResult SelectTags()
{
TagsModel tags = new TagsModel();
using (ProjectEntities db = new ProjectEntities())
{
tags.Tags = db.Tags.ToList<Tags>();
}
return View(tags);
}
Then on the Post I am getting:
'IEnumerable<TagsViewModel>' does not contain a definition for 'ToList' and the best extention method overload 'Enumerable.ToList<tags>(IEnumerable<Tags>)' requires a reciever of type 'IEnumerable<Tags>'
Here is the POST:
[HttpPost]
public ActionResult SelectTags(TagsModel model)
{
var selectedTags = model.Tags.Where(x => x.IsChecked == true).ToList<Tags>();
return Content(String.Join(",",selectedTags.Select(x => x.TagsId)));
}
I am using System.Link on the page.
Here are my Models:
namespace Project.ProjectModels.Entities
{
public class Tags
{
[Key]
public int TagsId { get; set; }
[Display(Name = "Id")]
public string Id { get; set; }
[Display(Name = "Address")]
public string DataHmiAddress { get; set; }
[Display(Name = "Type")]
public string DataHmiDataType { get; set; }
[Display(Name = "Round Places")]
public int DataHmiRoundPlaces { get; set; }
[Display(Name = "Update In")]
public int DataHmiUpdateIn { get; set; }
[Display(Name = "Update Level")]
public int DataHmiUpdateLevel { get; set; }
[Display(Name = "Value")]
[DisplayFormat(DataFormatString = "{0:0.000}", ApplyFormatInEditMode = true)]
public decimal DataHmiValue { get; set; }
}
}
namespace Project.ViewModels
{
public class TagsViewModel
{
public int TagsId { get; set; }
[Display(Name = "Id")]
public string Id { get; set; }
[Display(Name = "Address")]
public string DataHmiAddress { get; set; }
[Display(Name = "Type")]
public string DataHmiDataType { get; set; }
[Display(Name = "Round Places")]
public int DataHmiRoundPlaces { get; set; }
[Display(Name = "Update In")]
public int DataHmiUpdateIn { get; set; }
[Display(Name = "Update Level")]
public int DataHmiUpdateLevel { get; set; }
[Display(Name = "Value")]
[DisplayFormat(DataFormatString = "{0:0.000}", ApplyFormatInEditMode = true)]
public decimal DataHmiValue { get; set; }
public bool IsChecked { get; set; }
}
public class TagsModel
{
public virtual List<TagsViewModel> Tags { get; set; }
}
}
Thanks for your help!