Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC与Razor创建下拉列表_C#_Asp.net Mvc 3_Razor_Drop Down Menu - Fatal编程技术网

C# MVC与Razor创建下拉列表

C# MVC与Razor创建下拉列表,c#,asp.net-mvc-3,razor,drop-down-menu,C#,Asp.net Mvc 3,Razor,Drop Down Menu,我有下面的代码,我可以创建一个下拉列表,但是当我提交时,我得到一个对象引用,没有设置为对象异常的实例新闻类具有类别且类别类具有Id、名称、顺序 我怎样才能解决这个问题 我的看法是: <div class="editor-field"> @Html.DropDownListFor(m => m.News.Category.Id, Model.Categories, "Select One") @Html.ValidationMessageFor(m => m.New

我有下面的代码,我可以创建一个下拉列表,但是当我提交时,我得到一个对象引用,没有设置为对象异常的实例<代码>新闻类具有<代码>类别且类别类具有Id、名称、顺序

我怎样才能解决这个问题

我的看法是:

<div class="editor-field">
  @Html.DropDownListFor(m => m.News.Category.Id, Model.Categories, "Select One")
  @Html.ValidationMessageFor(m => m.News.Category)
 </div> 

保存模型
session.Save(newsViewModel.News)时出现异常

在哪里创建模型对象?在视图中使用Model.Categories,但不将模型对象传递给视图。应将模型对象作为视图方法的第一个参数传递:

    ...
    catch
    {
        return View(/* here, there must be a model object */);
    }
诸如此类:

    ...
    catch
    {
        var model = new NewsViewModel();
        return View(model);
        ... or ...
        return View(session.Load<NewsViewModel>(....));
    }
。。。
抓住
{
var模型=新的NewsViewModel();
返回视图(模型);
或
返回视图(session.Load(..);
}
试试这个

public int? SelectedId { get; set; }

@Html.DropDownListFor(m => m.SelectedId, Model.Categories, "--Select One--")

[HttpPost]
public ActionResult Create(NewsViewModel newsViewModel)
{
    if(ModelState.isValid())
    {
    try
    {
        using (var session = NHibernateHelper.OpenSession())
        {
            using (var tx = session.BeginTransaction())
            {
                newsViewModel.News.Category.Id = newsViewModel.SelectedId.Value;
                session.Save(newsViewModel.News); 
                tx.Commit();
            }
        }

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
    }
}

您的下拉列表中是否填充了get请求中的值

如果是,在提交时,viewModel的m.News.Category.Id属性是否设置为您在下拉列表中选择的值的Id


如果是,那么这不是下拉列表的问题…这与您正在使用的NHibernate会话有关…尝试类似(News)session.Save(newsViewModel.News)的操作

在我保存模型时,您的代码中哪里会出现此错误。session.Save(newsViewModel.News);但是您没有在会话中将此对象传递给View方法。或者您没有在视图代码中使用会话模型对象。您的下拉列表中是否填充了get请求中的值?如果是,在提交时,viewModel的m.News.Category.Id属性是否设置为您在下拉列表中选择的值的Id?如果是,那么这不是下拉列表的问题…这与您正在使用的NHibernate会话有关…尝试类似(News)session.Save(newsViewModel.News)的操作;这个…+1是为了反驳这个问题上不必要的-1…:)不过,我在获取类别对象时没有问题。它确实通过了。@DarthVader-不确定您对下拉列表有什么问题,那么,也许您可以在编辑中发布您得到的异常,并提供有关错误和其他更多详细信息。
    ...
    catch
    {
        var model = new NewsViewModel();
        return View(model);
        ... or ...
        return View(session.Load<NewsViewModel>(....));
    }
public int? SelectedId { get; set; }

@Html.DropDownListFor(m => m.SelectedId, Model.Categories, "--Select One--")

[HttpPost]
public ActionResult Create(NewsViewModel newsViewModel)
{
    if(ModelState.isValid())
    {
    try
    {
        using (var session = NHibernateHelper.OpenSession())
        {
            using (var tx = session.BeginTransaction())
            {
                newsViewModel.News.Category.Id = newsViewModel.SelectedId.Value;
                session.Save(newsViewModel.News); 
                tx.Commit();
            }
        }

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
    }
}