0

刚刚开始使用 MVC 并试图通过查看以下示例来完成此操作: http ://forums.asp.net/t/1670552.aspx

我不断收到此错误:

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

第 9 行:@using (Html.BeginForm("Index","Home",FormMethod.Post, new{id = "ID"})){
第 10 行:@Html.DropDownListFor(m=>m.id, new SelectList (Model.list, "id","name"),"selectThis")
第 11 行:}

这是代码:

模型类(愚蠢的名字,我知道):

这些位于仅用于存储模型的控制台应用程序中。

namespace Model
{
  public class Model
  {
    public int id { get; set; }
    public string name { get; set; }
  }

  public class List
  {
    public int id { get; set; }
    public List<Model> list = new List<Model>();
  }

  public class subModel
  {
    public int id { get; set; }
    public int modId { get; set; }
    public string name { get; set; }
  }

  public class subList
  {
    public List<subModel> list = new List<subModel>();
  }
}

控制器:(正在使用类中的方法填充 subList.list 和 List.list,但现在决定尝试这种方式,得到了同样的错误)

namespace DropboxTest.Controllers
{
public class HomeController : Controller
{
    //
    // GET: /Model/

    public ActionResult Index()
    {
        LoadModel();
        return View();
    }

    [ValidateInput(false)]
    [AcceptVerbs("POST")]
    public ActionResult Index([Bind(Exclude = "id")]Model.Model model)
    {
        var modId = Request["id"];
        LoadModel();
        LoadSubCategory(Convert.ToInt32(modId));

        return View();
    }

    public void LoadModel()
    {
        Model.List listM = new Model.List();
        listM.id = 0;
        Model.Model mod1 = new Model.Model();
        mod1.id = 1;
        mod1.name = "me";
        Model.Model mod2 = new Model.Model();
        mod2.id = 2;
        mod2.name = "me";
        listM.list.Add(mod1);
        listM.list.Add(mod2);

        ViewBag.Model = listM;
    }

    public void LoadSubCategory(int id)
    {
        Model.subList subList = new Model.subList();

        Model.subModel sub1 = new Model.subModel();
        Model.subModel sub2 = new Model.subModel();
        sub1.id = 1;
        sub1.name = "notme";
        sub1.modId = 1;
        sub2.id = 1;
        sub2.name = "notme";
        sub2.modId = 1;
        subList.list.Add(sub1);
        subList.list.Add(sub2);

        List<Model.subModel> sel = new List<Model.subModel>();
        foreach (var item in subList.list)
        {
            if (item.modId == id)
            {
                sel.Add(item);
            }
        }
        ViewBag.SubModel = sel;
    }
}
}

视图:(我不知道 subModel 下拉菜单的任何内容是否有效,因为我什至还没有进入该部分,但是 w/e。)

@model Model.List
@{
  ViewBag.Title = "Index";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@using (Html.BeginForm("Index","Home",FormMethod.Post, new{id = "ID"})){
   @Html.DropDownListFor(m=>m.id, new SelectList(Model.list, "id","name"),"selectThis")
}

@if (ViewBag.SubModel != null)
{
   @Html.DropDownList("SubModel",ViewBag.SubModel as SelectList, "select one")
}

这可能是一件非常愚蠢的事情,但我已经被困了几个小时尝试不同的事情。

PS:这只是一个测试应用程序。在我看到它是如何完成的之后,我将使用 SQL DB 做一个,使用 ConsoleApplications 中的模型从 DB 中检索和存储数据并将其显示在视图中,因此任何关于此的建议也将不胜感激。

非常感谢所有读到这里的人,祝你有美好的一天。

4

1 回答 1

0

您永远不会将模型传递给控制器​​中的视图,您只需存储在ViewBag.Model.

尝试如下操作:

[ValidateInput(false)]
[AcceptVerbs("POST")]
public ActionResult Index([Bind(Exclude = "id")]Model.Model model)
{
    var modId = Request["id"];

    //get model
    var model = LoadModel();

    //pass it to the view
    return View(model);
}
于 2013-09-19T21:40:51.200 回答