带有类值的下拉列表
本文关键字:下拉列表 | 更新日期: 2023-09-27 18:08:50
My class:
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
类。在Create View中发送值Id
,显示值model_name
。我该怎么做呢?控制器用户Nhibernate。Nhibernate是完成映射的地方。但基本上所有的功能和视图管理都在这里。
您需要创建SelectListItem
s:
public IEnumerable<SelectListItem> GetCarModels()
{
var list = new List<SelectListItem>();
foreach (var car in GET_CARS())
{
list.Add(new SelectListItem()
{
Value = car.Id.ToString(),
Text = car.model_name
});
}
return list;
}
作为第二个参数传递给DropDownListFor
:
@Html.DropDownListFor(m => m.Modelis, LIST_FROM_ABOVE)
更短的版本可能是:
public SelectList GetCarModels()
{
return new SelectList(GET_CARS(), "Id", "model_name")
}
和View:
@Html.DropDownListFor(m => m.Modelis, CarModels)