我的课:
public class Transport
{
public virtual int Id { get; set; }
public virtual Class.CarModel Modelis { get; set; }
}
和
public class CarModel
{
public virtual int Id { get; set; }
public virtual string model_name { get; set; }
}
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Linq;
using web_nt.Models;
using System.Web.Mvc;
using Npgsql;
namespace web_nt.Controllers
{
public class TransportController : Controller
{
public ActionResult Index()
{
using (ISession session = NHIbernateSession.OpenSession())
{
//ICriteria.SetMaxResult();
var index = session.Query<Transport>().ToList() ;
return View(index);
}
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Transport item)
{
try
{
using (ISession session = NHIbernateSession.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
// item.owner = item.Vartotojas.Id;
//item.travel_type = item.Vartotojas.name;
session.Save(item);
transaction.Commit();
}
}
return RedirectToAction("Index");
}
catch (Exception exception)
{
return View();
}
}
public ActionResult Edit(int id)
{
using (ISession session = NHIbernateSession.OpenSession())
{
var item = session.Get<Transport>(id);
return View(item);
}
}
[HttpPost]
public ActionResult Edit(int id, Transport item)
{
try
{
using (ISession session = NHIbernateSession.OpenSession())
{
var tabletoUpdate = session.Get<Transport>(id);
tabletoUpdate.model = item.model;
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(tabletoUpdate);
transaction.Commit();
}
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Details(int id)
{
using (ISession session = NHIbernateSession.OpenSession())
{
var item = session.Get<Transport>(id);
return View(item);
}
}
public ActionResult Delete(int id)
{
using (ISession session = NHIbernateSession.OpenSession())
{
var item = session.Get<Transport>(id);
return View(item);
}
}
[HttpPost]
public ActionResult Delete(int id, Transport item)
{
try
{
using (ISession session = NHIbernateSession.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Delete(item);
transaction.Commit();
}
}
return RedirectToAction("Index");
}
catch (Exception exception)
{
return View();
}
}
}
}
我想使DropDownListFor
类Modelis
。 你送价值Id
和展示价值model_name
在创建视图。 我该怎么做? 控制器用户NHibernate的。 NHibernate的就是映射完成。 但bassicly所有功能和管理的意见在这里。