DropDownList的与类值 [英] DropDownList with Class values
本文介绍了DropDownList的与类值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的类别:
公共类运输
{
公共虚拟INT标识{搞定;组; }
公共虚拟Class.CarModel Modelis {搞定;组; }
}
和
公共类CarModel
{
公共虚拟INT标识{搞定;组; }
公共虚拟字符串MODEL_NAME {搞定;组; }
}
控制器:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用NHibernate;
使用NHibernate.Linq;
使用web_nt.Models;
使用System.Web.Mvc;
使用Npgsql;命名空间web_nt.Controllers
{
公共类TransportController:控制器
{
公众的ActionResult指数()
{
使用(ISession的会话= NHIbernateSession.OpenSession())
{
//ICriteria.SetMaxResult();
VAR指数= session.Query<&GT运输()了ToList()。
返回查看(指数);
} }
公众的ActionResult的Create()
{
返回查看();
}
[HttpPost]
公众的ActionResult创建(交通运输项目)
{
尝试
{
使用(ISession的会话= NHIbernateSession.OpenSession())
{
使用(ITransaction事务= session.BeginTransaction())
{
// item.owner = item.Vartotojas.Id;
//item.travel_type = item.Vartotojas.name;
session.Save(项目);
器transaction.commit();
}
} 返回RedirectToAction(「指数」);
}
赶上(例外的例外)
{
返回查看();
}
} 公众的ActionResult编辑(INT ID)
{
使用(ISession的会话= NHIbernateSession.OpenSession())
{
VAR项目= session.Get<&GT运输(ID);
返回查看(项目);
} }
[HttpPost]
公众的ActionResult编辑(INT ID,运输项)
{
尝试
{
使用(ISession的会话= NHIbernateSession.OpenSession())
{
变种tabletoUpdate = session.Get≤(ID);和GT运输;
tabletoUpdate.model = item.model;
使用(ITransaction事务= session.BeginTransaction())
{
session.Save(tabletoUpdate);
器transaction.commit();
}
}
返回RedirectToAction(「指数」);
}
抓住
{
返回查看();
}
}
公众的ActionResult详细信息(INT ID)
{
使用(ISession的会话= NHIbernateSession.OpenSession())
{
VAR项目= session.Get<&GT运输(ID);
返回查看(项目);
}
} 公众的ActionResult删除(INT ID)
{
使用(ISession的会话= NHIbernateSession.OpenSession())
{
VAR项目= session.Get<&GT运输(ID);
返回查看(项目);
}
} [HttpPost]
公众的ActionResult删除(INT ID,运输项)
{
尝试
{
使用(ISession的会话= NHIbernateSession.OpenSession())
{
使用(ITransaction事务= session.BeginTransaction())
{
使用Session.delete(项目);
器transaction.commit();
}
}
返回RedirectToAction(「指数」);
}
赶上(例外的例外)
{
返回查看();
}
}
}
}
和我想要 DropDownListFor
对于类 Modelis
。您发送的创建视图值编号
和示值模型名称
。我该怎么办呢?
控制器用户NHibernate的。 Nhibernate的是,其中映射完成。但bassicly所有功能和管理的意见在这里。
解决方案
您需要创建的 SelectListItem
小号:
公开的IEnumerable< SelectListItem> GetCarModels()
{
VAR名单=新名单,LT; SelectListItem>(); 的foreach(在GET_CARS VAR车())
{
list.Add(新SelectListItem()
{
值= car.Id.ToString(),
文字= car.model_name
});
} 返回列表;
}
这则作为传递的第二个参数 DropDownListFor
@ Html.DropDownListFor(M = GT; m.Modelis,LIST_FROM_ABOVE)
My Class:
public class Transport
{
public virtual int Id { get; set; }
public virtual Class.CarModel Modelis { get; set; }
}
And
public class CarModel
{
public virtual int Id { get; set; }
public virtual string model_name { get; set; }
}
Controller:
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();
}
}
}
}
And I want to make DropDownListFor
for Class Modelis
. You send value Id
and show value model_name
in Create View. How can I do it?
Controller users Nhibernate. Nhibernate is where mapping is done. But bassicly all functions and Views management is here.
解决方案
You need to create 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;
}
This you then pass as the second parameter to DropDownListFor
:
@Html.DropDownListFor(m => m.Modelis, LIST_FROM_ABOVE)
这篇关于DropDownList的与类值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文