News Details in MVC

Last Reply 6 days ago By kalpesh

Posted 6 days ago

Actully i Have Two Classes one for saving the News and the other for Saving the photos of the news below are the classes which i used

 

 

public class News
    {
      

        public int Id { get; set; }
        public string Title { get; set; }
        public string Details { get; set; }

        public IEnumerable<Photos> Photos { get; set; }


    }

 

    public class Photos
    {
        public int Id { get; set; }
        public string Path { get; set; }
        public string FileName { get; set; }

        [NotMapped]
        public HttpPostedFile PosteImages { get; set; }
    }

 

and i have created a viewModel named(NewsViweModel) it is job is to retrieve the photos and the news from two different table from Db

 

this is the ViewModel and the Action Controller and the Form as well also 

 

 

    public class NewsViweModel
    {
        public News News { get; set; }
        public List<Photos> Photos { get; set; }
    }

 

        public ActionResult NewsDetails(int id)
        {
            var photos = _context.Photos.Where(i => i.Id == id).ToList();

            var NewsDetails = new NewsViweModel
            {
                News = new News(),
                Photos = photos
            };

            
            return View(NewsDetails);
        }

 

@model IEnumerable<NHP_Website.ViewModels.NewsViweModel>
@{
    ViewBag.Title = "NewsDetails";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<div class="row">
    <h2>@Model.News.Title</h2>
    <p>@Model.News.Title</p>
    @foreach (var news in Model)
    {
     <img  src="@news.Photos.path"/>
    }


</div>

 

the probleme is in the view 

No1: i am not able to see the properties of Photos class inside the foreach block

No2: without itteration inisde <h2> tag and inside <p> tag i want show the news title and the news details 

 

 

thanks

You are viewing reply posted by: kalpesh 6 days ago.
I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html