I'd like to edit and add data from multiple tables. I started as said in this solution: How do I display data from multiple tables in a single MVC view
I had 3 models: uzytkownik, rolaPowiazanie and rola. Between "uzytkownik" and "rolaPowiazanie" is connection "idUzytkownik", between "rolaPowiazanie" and "rola" - "rolaId". I'd like to show in a table information from "uzytkownik" and "rola".
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.nazwaRoli)
</th>
(...)
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.nazwaRoli)
</td>
(...)
DisplayNameFor gives "nazwaRoli" but DisplayFor(modelItem => item.nazwaRoli) gives "idRola". When I want to see in the view items from idRola, there was idRola too.
I add this into controller:
public ActionResult Index()
{
IEnumerable<UzytkownicyRola> model = null;
List<UzytkownicyRola> verify = new List<UzytkownicyRola>();
var connection = System.Configuration.ConfigurationManager.ConnectionStrings["AplikacjaContext"].ConnectionString;
using (var cn = new SqlConnection(connection))
{
cn.Open();
if (cn.State == ConnectionState.Open)
{
string query = "select uzytkownik.idUzytkownik,uzytkownik.imie,uzytkownik.nazwisko,uzytkownik.pesel,uzytkownik.haslo,uzytkownik.email,rolaPowiazanie.idUzytkownik,rolaPowiazanie.idRola,rola.idRola,rola.nazwaRoli from uzytkownik inner join rolaPowiazanie on uzytkownik.idUzytkownik=rolaPowiazanie.idUzytkownik inner join rola on rolaPowiazanie.idRola=rola.idRola;";
SqlCommand cmd = new SqlCommand(query, cn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
verify.Add(new UzytkownicyRola { idUzytkownik = Convert.ToInt32(dr[0].ToString()), imie = dr[1].ToString(), nazwisko = dr[2].ToString(), pesel = dr[3].ToString(), haslo = dr[4].ToString(), email = dr[5].ToString(), idRolaPowiazanie = Convert.ToInt32(dr[6].ToString()), idRola = Convert.ToInt32(dr[7].ToString()), nazwaRoli = dr[8].ToString() });
}
}
cn.Close();
return View(verify);
}
}
And in the view, when I want to show "nazwaRoli" i get "idRola". What should I change to correct show "nazwaRoli"?
Automatically generated Edit look's like:
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
UzytkownicyRola uzytkownicyRola = db.UzytkownicyRolas.Find(id);
if (uzytkownicyRola == null)
{
return HttpNotFound();
}
return View(uzytkownicyRola);
}
--EDIT-- I also have got a problem with Edit and Details.
There's an error:
System.Data.SqlClient.SqlException: Invalid object name 'dbo.UzytkownicyRolas'.
Error source:
UzytkownicyRola uzytkownicyRola = db.UzytkownicyRolas.Find(id);
"UzytkownicyRolas" exist only in AplikacjaContext (DbContext created by me earlier) and this is in DAL folder. How can I change this to this method works?
AplikacjaContext'c code:
public partial class AplikacjaContext : DbContext
{
public AplikacjaContext()
: base("name=AplikacjaContext")
{
}
public virtual DbSet<rola> rola { get; set; }
public virtual DbSet<rolaPowiazanie> rolaPowiazanie { get; set; }
public virtual DbSet<uzytkownik> uzytkownik { get; set; }
public System.Data.Entity.DbSet<Aplikacja.Models.UzytkownicyRola> UzytkownicyRolas { get; set; }
}
The ordinal number: 8 on dr (:SqlDataReader) will get "idRola" in your sql.
select
uzytkownik.idUzytkownik,--0
uzytkownik.imie,--1
uzytkownik.nazwisko,--2
uzytkownik.pesel,--3
uzytkownik.haslo,--4
uzytkownik.email,--5
rolaPowiazanie.idUzytkownik,--6
rolaPowiazanie.idRola,--7
rola.idRola,--8
rola.nazwaRoli --9
...
Using column name on SqlDataReader will be more readable, like this...
nazwaRoli = dr["nazwaRoli"].ToString();
Please check that you have the right reference on the class : "UzytkownicyRola", the error is not on "UzytkownicyRola s "
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.