我的班級:的DropDownList與階級的價值觀
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是完成映射的地方。但幾乎所有的功能和視圖管理都在這裏。
統計員。但是這並沒有什麼幫助,因爲我無法在視圖中聲明兩個模型。 – user1883184
我沒有任何意義,你嘗試做什麼。但我認爲你必須嘗試第一次根據通行證編號第二下拉填充,所以你嘗試使用jQuery。 –
我通過使用nhibernate從SQL數據庫映射獲得CarModels – user1883184