DropDownList的与类值(DropDownList with Class values)

2019-10-20 07:04发布

我的课:

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();
        }
    }
}

}

我想使DropDownListForModelis 。 你送价值Id和展示价值model_name在创建视图。 我该怎么做? 控制器用户NHibernate的。 NHibernate的就是映射完成。 但bassicly所有功能和管理的意见在这里。

Answer 1:

您需要创建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)


Answer 2:

一个较短的版本可能是:

public SelectList GetCarModels()
{
    return new SelectList(GET_CARS(), "Id", "model_name")
}

并在视图:

@Html.DropDownListFor(m => m.Modelis, CarModels)


文章来源: DropDownList with Class values
标签: c# asp.net razor