mercredi 1 février 2017

How to implictly convert from one datatype in a class to another in asp.net mvc?

How to implicitly convert from one datatype in a class to another in asp.net mvc. How can I change my error in the controller?

models

public class ClsNews
{
    public int Id { get; set; }
    public string Description { get; set; }
}
public class NewsList
{
    public int Id { get; set; }
    public string description { get; set; }
    public List<News> NewsLst { get; set; }
}
public partial class News
{
    public int Id { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public string Description { get; set; }
    public Nullable<bool> IsActive { get; set; }
}

view

<table class="table">
    @{int i = 1;}
    @for (var j = 0; j < Model.NewsLst.Count; j++)
    {
        <tr>

            <td>
                @Html.HiddenFor(m => Model.NewsLst[j].Id)
                @Html.HiddenFor(m => Model.NewsLst[j].Description)
                @Html.DisplayFor(m => Model.NewsLst[j].Description.Substring(0,20))
                @Html.ActionLink("Read More", "NewsInnerPage", "News")

            </td>
        </tr>
        i++;
    }
</table>

this is my Controller

News news = await db.News.FindAsync(id);
NewsList nl = new NewsList();
nl.Id = id;

nl.description = (db.News.Where(h => h.Id == id)).Count() > 0 ?
                    (db.News.Where(h => h.Id == id)).First().Description : string.Empty;

var res = db.News.Where(s => s.Id == id).Select(t => new ClsNews 
{
    Id = t.Id,
    Description = t.Description
}).ToList();
nl.NewsLst = res;
if (news == null)
{
    return HttpNotFound();
}
return View(nl);

The error is generating in the controller where the value in variable res is attaching to the NewsLst. How can I correct my error?

Aucun commentaire:

Enregistrer un commentaire