我很难弄清楚如何将数据从数据库绑定到ViewModel。基本上,我有一个域模型,我发现它有太多的属性,我想减少逻辑上我选择ViewModel这样做。
域模型(从数据库自动创建):
public partial class Ticket
{
public Ticket()
{
this.Daily = new HashSet<Daily>();
this.Ticket1 = new HashSet<Ticket>();
}
public int idTicket { get; set; }
public Nullable<int> idNadredeniTicket { get; set; }
public short RedniBroj { get; set; }
public int idFirma { get; set; }
public Nullable<int> idKontakt { get; set; }
public Nullable<int> idManager { get; set; }
public string Tip { get; set; }
public string Status { get; set; }
public Nullable<System.DateTime> DatumPrijave { get; set; }
public string VrstaPrijave { get; set; }
public string Prioritet { get; set; }
public Nullable<System.DateTime> DatumDo { get; set; }
public string Opis { get; set; }
public string Biljeske { get; set; }
public Nullable<bool> Zatvoren { get; set; }
public Nullable<bool> IzdanRacun { get; set; }
public Nullable<System.DateTime> DatumZatvaranja { get; set; }
public Nullable<int> idAsset { get; set; }
public virtual ICollection<Daily> Daily { get; set; }
public virtual Firma Firma { get; set; }
public virtual Kontakt Kontakt { get; set; }
public virtual Kontakt Kontakt1 { get; set; }
public virtual ICollection<Ticket> Ticket1 { get; set; }
public virtual Ticket Ticket2 { get; set; }
}
视图模型:
public class OpenTickets
{
public int idTicket { get; set; }
public Nullable<int> idNadredeniTicket { get; set; }
public short RedniBroj { get; set; }
public int idFirma { get; set; }
public Nullable<int> idKontakt { get; set; }
public Nullable<int> idManager { get; set; }
public string Tip { get; set; }
public string Status { get; set; }
public Nullable<System.DateTime> DatumPrijave { get; set; }
public string VrstaPrijave { get; set; }
public string Prioritet { get; set; }
public string Opis { get; set; }
public string Biljeske { get; set; }
public string BrojTicketa
{
get
{
return idNadredeniTicket.ToString() + "-" + RedniBroj.ToString();
}
}
public string NazivTicketa
{
get
{
return BrojTicketa + " - " + Opis;
}
}
public string DetaljiTicketa
{
get
{
return Opis + "\r\n" + Biljeske;
}
}
}
我想要完成的是通过查询将数据从数据库绑定到ViewModel,但理解上,我收到有关传递给View的不同类型对象的错误。我正在发布控制器和视图作为参考。
控制器
public ActionResult OpenTickets()
{
var openTickets = db.Ticket
.Where(t => t.idFirma == 1)
.Where(t => t.Zatvoren == false);
return View(openTickets.ToList());
}
查看(为简洁起见,故意省略某些代码)
@model IEnumerable<IDE3_CRM.ViewModels.OpenTickets>
<table>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Biljeske)</td>
<td>@Html.DisplayFor(modelItem => item.Opis)</td>
</tr>
}
</table>
答案 0 :(得分:1)
我建议在Repository
中包装您的数据库调用。在这里,您可以将数据库对象转换为视图模型。例如:
public ActionResult OpenTickets()
{
var openTickets = ticketRepo.GetOpenTickets();
return View(openTickets);
}
// Implementation of ITicketRepo
public IEnumerable<OpenTickets> GetOpenTickets()
{
return db.Ticket
.Where(t => t.idFirma == 1 && t.Zatvoren == false)
.Select(do => new OpenTickets
{
// Fill in view model properties from database object
});
}
答案 1 :(得分:1)
Hrvach,
您可以限制视图本身的数据字段,我认为可能更有效。这里说的是另一种方法:
返回打开的门票列表
public ActionResult OpenTickets()
{
List<OpenTickets> openTicketList = new List<OpenTickets>();//create a list of openTickets
var Tickets = db.Ticket//select the tickets that you want
.Where(t => t.idFirma == 1)
.Where(t => t.Zatvoren == false);
foreach (var ticket in Tickets)//Loop over the tickets and create an openTicket out of each ticket then add the openTick to the openTicketList
{
OpenTickets openTicket = new OpenTickets();//create new OpenTickets object
openTicket.propery1 = ticket.propery1;//set each property of the openTicket equal to the property of the Ticket that you want to keep
openTicket.propery2 = ticket.propery2;
openTicket.propery3 = ticket.propery3;
openTicket.propery4 = ticket.propery4;
openTicketList.Add(openTicket);//add new OpenTickets object to the list
}
return View(openTicketList);
}
我希望这会有所帮助...祝福 比尔