简体   繁体   English

MVC与Razor创建下拉列表

[英]MVC with Razor creating drop down list

I have the follwing code, I m able to create a drop down list but when I submit, I get an Object reference not set to an instance of an object exception. 我有以下代码,我能够创建一个下拉列表但是当我提交时,我得到的Object引用没有设置为对象异常的实例。 News class has Category and Category class have Id, Name, Order. News类有Category和Category类有Id,Name,Order。

How can I fix this? 我怎样才能解决这个问题?

My view: 我的观点:

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

The view model: 视图模型:

public class NewsViewModel
{
    public string SelectedCategoryId { get; set; }
    public IEnumerable<SelectListItem> Categories { get; set; }
    public News News { set; get; }
}

And the controller action: 和控制器动作:

[HttpPost]
public ActionResult Create(NewsViewModel newsViewModel)
{
    try
    {
        using (var session = NHibernateHelper.OpenSession())
        {
            using (var tx = session.BeginTransaction())
            {

                session.Save(newsViewModel.News);
                tx.Commit();
            }
        }

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

I m getting the exception while saving the model session.Save(newsViewModel.News); 我在保存模型session.Save(newsViewModel.News);时获得异常session.Save(newsViewModel.News);

Is your dropdown being populated with values on your get request? 您的下拉列表是否在您的获取请求中填充了值?

if yes, on submit, is the viewModel's m.News.Category.Id property is set with the id of the value you've selected in the dropdown? 如果是,则在提交时,viewModel的m.News.Category.Id属性是否设置了您在下拉列表中选择的值的ID?

if yes, then its not the problem with the dropdown...it is something to do with the NHibernate session that you are using...try something like (News)session.Save(newsViewModel.News); 如果是,那么它不是下拉列表的问题......它与您正在使用的NHibernate会话有关...尝试类似(新闻)session.Save(newsViewModel.News);

Where do you create the model object? 你在哪里创建模型对象? You use Model.Categories in the view, but you don't pass the model object to the view. 您在视图中使用Model.Categories,但不将模型对象传递给视图。 You should pass the model object as the first parameter of the View method: 您应该将模型对象作为View方法的第一个参数传递:

    ...
    catch
    {
        return View(/* here, there must be a model object */);
    }

Something like that: 像这样的东西:

    ...
    catch
    {
        var model = new NewsViewModel();
        return View(model);
        ... or ...
        return View(session.Load<NewsViewModel>(....));
    }

Try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM