【问题标题】:MVC3 using Drop Down List model and other models in the same viewMVC3 在同一视图中使用下拉列表模型和其他模型
【发布时间】:2012-11-21 06:18:05
【问题描述】:

我正在使用this example 开发一个下拉列表。在我对视图中调用模型的方式进行一些更改之前,它运行良好。下拉列表模型类称为 dropdownModel。因为我的视图包含 2 个模型,所以我创建了一个“大”模型类 BigModelClass 来保存我的两个模型。

大模型是这样的

public class BigModelClass {
   public DropDownModel dropDownModel { get; set; }
   public IEnumerable<projectname.Model.model2> var2 { get; set; }
}

在我看来,我将模型称为:

@model BigModel

现在在我看来,我调用使用下拉列表如下:

@Html.LabelFor(m => m.dropDownModel.State)
@Html.DropDownListFor(m => m.dropDownModel.State,
                 new SelectList(Model.dropDownModel.StateList, "Value", "Text"))
<span class="required"></span>
@Html.ValidationMessageFor(m => m.dropDownModel.State)

很遗憾,我收到以下错误:

System.NullReferenceException:对象引用未设置为对象的实例。

上线

@Html.DropDownListFor(m => m.dropDownModel.State, new SelectList(Model.dropDownModel.StateList, "Value", "Text"))

如果我只使用 dropDownModel 模型,一切都可以正常工作。

非常感谢任何帮助

编辑 视图的控制器:

public ActionResult Index(){
   return View (new BigModelClass());
}

【问题讨论】:

  • 发布你的控制器的代码,问题可能是你如何实例化模型。
  • 您正在使用 m.dropDownModel 但我在 BigModel 中看不到 dropDownModel 的定义
  • @Johann & Behnam:请参阅编辑。
  • @jpo 你在哪里初始化 Model.dropDownModel ?
  • 你的确切问题是这个!你可以在构造函数中初始化它。

标签: asp.net-mvc-3 view drop-down-menu model visual-studio-2012


【解决方案1】:

假设您直接从该示例复制了 DropDownModel,您需要向 BigModelClass 添加一个构造函数并在那里实例化 dropDownModel。

public class BigModelClass {
   public DropDownModel dropDownModel { get; set; }
   public IEnumerable<projectname.Model.model2> var2 { get; set; }

   public BigModelClass() {
      dropDownModel = new DropDownModel();
   }
}

或者,在您的控制器中,实例化下拉模型:

public ActionResult Index(){
   return View (new BigModelClass {
         dropDownModel = new DropDownModel()
   });
}

【讨论】:

    【解决方案2】:

    您的Model.dropDownModel 很可能为空,我很确定您没有在默认构造函数BigModelClass() 中实例化它。当属性定义没问题时 m =&gt; m.dropDownModel.State 它无法返回项目集合的实例:Model.dropDownModel.StateList

    【讨论】:

    • 您需要推送(或拉取,如果它适合您的项目),项目集合到“Model.dropDownModel.StateList”,例如将其传递给您的 BigModelClass(items) 重载。
    【解决方案3】:

    这个问题是因为你没有绑定数据到下拉列表。您必须将数据绑定到控制器操作中的下拉列表。如果您在控制器操作上绑定数据,请确保它也绑定在 [httppost] 控制器操作中,因为 modelstate.valid 为 false。

        public ActionResult Register()
        {
            RegisterModel model = new RegisterModel();
            List<SequrityQuestion> AllSequrityQuestion = new List<SequrityQuestion>();
            model.SequrityQuestions = GetAllSequrityQuestion();
    
            return View(model);
        }
    
         [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                // there was a validation error =>
                // rebind categories and redisplay view
    
                model.SequrityQuestions = GetAllSequrityQuestion();
            }
            if (ModelState.IsValid)
            {
                // Your code to post
            }
    
            return View(model);
        }
    

    在上面的例子中,注册模型中有一个名为 SequrityQuestions 的下拉列表。我认为这就是你面临这个问题的原因。一定要在modelstate.valid false的情况下绑定数据到下拉列表,那么你的问题就会消失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多