Witam
Uczę sie technologii .Net MVC 5.
W ramach swojego szkolenia tworzę aplikację w której mam wydzielone repozytorium z metodami gdzie wykonywane są wszelkie operacje na bazie i modelach.
Chciałbym aby ViewModel zawierał jako jedna z właściwości "stronicowaną listę", a pozostałe właściwości żeby przechowywały pojedyncze informacje.
Mam problem z dostępem do listy, która jest właściwością obiektu ViewModel. Czy ktoś mógłby spojrzeć na poniższy kod i nakierunkować mnie co mam poprawić aby to zadziałało?
Poniżej przedstawię to co naskrobałem.
Model DB:
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace Repo.Models
{
public class MContact
{ [Display(Name = "Contact ID")]
public int Id { get; set; }
[Required(ErrorMessage = "Proszę podać imię")]
[RegularExpression("^[a-zA-Z]+", ErrorMessage ="Tylko litery")]
[Display(Name = "First Name")]
[MinLength(2, ErrorMessage = "Minimalna długość znaków wynosi 2")]
[MaxLength(50, ErrorMessage = "Maksymalna długość znaków wynosi 50")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required(ErrorMessage = "Proszę podać nazwisko")]
[RegularExpression("^[a-zA-Z]+$", ErrorMessage = "Tylko litery")]
[MinLength(2, ErrorMessage = "Minimalna długość znaków wynosi 2")]
[MaxLength(50, ErrorMessage = "Maksymalna długość znaków wynosi 50")]
public string LastName { get; set; }
[Display(Name ="Phone Number")]
public string PhoneNumber { get; set; }
[EmailAddress(ErrorMessage = "Wpisz prawidłowy adres email")]
public string Email { get; set; }
public string Address { get; set; }
public string City { get; set; }
[RegularExpression("^[0-9]{2}-[0-9]{3}$", ErrorMessage = "Niepoprawny kod pocztowy")]
public string Zip { get; set; }
public bool IsFriend { get; set; }
}
public class DBContacts : DbContext
{
public DbSet<MContact> MContact { get; set; }
}
}
ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PagedList;
namespace Repo.ViewModels
{
public class ContactsViewResponse
{
public string Sort { get; set; }
public IPagedList ViewPagedList { get; set; }
}
}
**
Kontroler:**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using Repo.Repository;
using Repo.Models;
namespace ContactsBook_v2.Controllers
{
public class HomeController : Controller
{
ContactsRepository ContactsDataMethods = new ContactsRepository();
public ActionResult Contacts(int? page)
{
var CResponse = ContactsDataMethods.ContactsResponse(page);
return View(CResponse);
}
}
View:
@using PagedList.Mvc;
@model Repo.ViewModels.ContactsViewResponse
@*poniższy sposób dostępu do elementów listy nie dziala*@
@foreach (var item in Model.ViewPagedList)
{
<tr>
<td>@item.Id</td>```csharp
<td>@item.FirstName</td>
<td>@item.LastName</td>
<td>@item.PhoneNumber</td>
<td>@item.Email</td>
<td>@item.City</td>
<td>@item.Address</td>
<td>@item.Zip</td>
<td>@item.IsFriend</td>
</tr>
}
Repozytorium:
using System;
using System.Collections.Generic;
using System.Linq;
using Repo.Models;
using System.Web.Mvc;
using System.Data.Entity;
using Repo.ViewModels;
using PagedList;
namespace Repo.Repository
{
public class ContactsRepository
{
public DBContacts baza;
public ContactsRepository()
{
baza = new DBContacts();
}
public ContactsViewResponse ContactsResponse(int? pageNumber)
{
var ContactsDBData = baza.MContact;
int PageSize = 5;
int PageNumber = (pageNumber ?? 1);
ContactsViewResponse ContactsResponse = new ContactsViewResponse();
ContactsResponse.ViewPagedList = ContactsDBData.ToPagedList(PageNumber, PageSize);
ContactsResponse.Sort = "Test";
return ContactsResponse;
}
}