I need show record from database. But I have a error:
‘object’ does not contain a definition for ‘Login’
View:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Dodaj Pracownika</legend>
@foreach (var pr in ViewBag.dane)
{
<div>Login: @pr.Login</div>
<div>Imie: @pr.imie</div>
}
</fieldset>
}
Controller:
public ActionResult detail(int LoginID)
{
var user = (from p in baza.Logowanies
join d in baza.uzytkowniks on p.LoginID equals d.LoginID
where LoginID == p.LoginID
select new { p.Login, p.Haslo, p.konto, d.imie, d.nazwisko, d.pesel, d.nip, d.telefon, d.adres_zamieszkania, d.email }).ToList();
ViewBag.dane = user;
return View();
}
It was working when I put records from one table in database (without join in query). But now is something wrong.
Using
@grid.GetHtml
everything is allright.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EDIT
I make edits and now I have:
View:
@model Restauracja.Models.pracownikModel
@foreach (dynamic pr in ViewBag.dane)
{
<div>Login: @pr.Login</div>
<div>Imie: @pr.imie</div>
Model:
public class pracownikModel
{
public LoginModel LoginModel { get; set; }
public uzytkownikModel uzytkownikModle { get; set; }
public pracownikModel() {
LoginModel = new LoginModel();
uzytkownikModle = new uzytkownikModel();
}
}
public class LoginModel
{
[Required]
public string Użytkownik { get; set; }
[Required]
public string Hasło { get; set; }
public string Konto { get; set; }
}
public class uzytkownikModel
{
[Required]
public string imie { get; set; }
[Required]
public string nazwisko { get; set; }
. . . . .
}
And Controller:
public ActionResult Kontakt()
{
bazaDataContext baza = new bazaDataContext();
var user = (from p in baza.Logowanies
join d in baza.uzytkowniks on p.LoginID equals d.LoginID
select new pracownikModel{p.Login, d.imie}).ToList();
ViewBag.dane = user;
return View(user);
}
In controller in select I have
select new pracownikModel{p.Login, d.imie})
and I cant add
uzytkownik = p.Login, imie = d.imie
because insted uzytkownik and imie i have LoginModel or uzytkonikModel.
I have
select new pracownikModel{…}
and I have error:
Cannot initialize type ‘Restauracja.Models.pracownikModel’ with a collection initializer because it does not implement ‘System.Collections.IEnumerable’
You’ve changed to use an anonymous object so the view doesn’t know what type it is and defaults to the
objecttype. You could usedynamicinstead ofvaror you could select into a class with the same properties as the anonymous object in which case it would be able to determine the type and know that it had those properties.Or
With the model class
Any reason why you’re not using a view model rather than setting data on the ViewBag?
Action
View