MVC 5 NHibernate DropdownlistFor Object引用未设置为对象的实例

时间:2016-12-09 04:55:21

标签: c# asp.net asp.net-mvc nhibernate fluent-nhibernate

我遇到了一个大问题,无论如何都无法解决:( 我使用mvc5和nhibernate与MySql。 在创建时我的代码达到了这一点:

var tosave = (Site) site2; //Site is mymain model but there are some properties not available on it so i made another model using inheritance plus              adding the needed properties so when saving i cast it to the original Site model. 
session.Save(tosave);
transaction.Commit();

我得到了这个例外:

  

对象引用未设置为对象的实例

@Html.DropDownListFor(model => model.id_departamento, new SelectList(Model.Departamentos, "id_departamento", "descripcion"))

GET的控制器:站点/创建

public ActionResult Create()
{
    using (ISession session = NhibernateHelper.OpenSession())
    {
        var deptos = session.Query<Departamentos>().ToList();
        .... // more code
        var instanciadelacopia=new CopiaSitio
        {
            .... // more code
            Departamentos = deptos, 
        };
        return View(instanciadelacopia);
    }
}

该特定部分的观点

<div class="form-group">
    <label class="control-label col-md-2"> Departamento </label>
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.id_departamento, new   SelectList(Model.Departamentos, "id_departamento", "descripcion"))
    </div>
</div>

发布后是

[HttpPost]
public ActionResult Create(CopiaSitio sitio2)
{
    try
    {
        using (ISession session = NhibernateHelper.OpenSession()) 
        {
            using (ITransaction transaction = session.BeginTransaction()) 
            {
                .... // more code
                var tosave = (Sitios) sitio2;
                session.Save(tosave);
                transaction.Commit();
            }
        } 
        return RedirectToAction("Index");
    }
    catch (Exception exception)
    {
        return View();
    }
}

任何帮助都会非常感激,被困在这里好几天......

0 个答案:

没有答案