制作两个下拉列表时,没有“ IEnumerable”类型的ViewData项

时间:2019-03-15 21:47:30

标签: asp.net model-view-controller dropdown ienumerable viewdata

我试图在视图中包含两个下拉列表,然后包含我的所见即所得,但我一直抛出错误“ System.InvalidOperationException:'没有类型为'IEnumerable'的ViewData项。如果我注释了'public ActionResult,索引”,将public ActionResult GetCat重命名为public ActionResult索引,它可以工作并为我的Category Type创建一个下拉列表。但是如果我都使用真实名称(控制器中的public ActionResult索引和public ActionResult GetCat)运行,因此,每个代码都可以单独工作,但是放在一起时,我得到了异常。我认为问题出在公共Action GetCat中,但我无法弄清楚。找到答案。我是ASP.Net和MVC的新手。这是我的代码。非常感谢您的帮助。

 < Controller>
 public class AdministratorController : Controller
{
    //GET: Administrator
    public ActionResult Index()
    {
        var set = new Services.SqlHelper().GetSet(@"
                SELECT UserId,
                       FirstName

                FROM Users
            ");

        var userList = new List<SelectListItem>();

        if (set.Tables != null && set.Tables.Count > 0)
        {
            var users = set.Tables[0];

            foreach (DataRow row in users.Rows)
            {
                userList.Add(new SelectListItem()
                {
                    Text = row["FirstName"].ToString(),
                    Value = row["UserId"].ToString()
                });
            }
        }

        return View(new AdministratorViewModel()
        {
            Users = userList
        });



    }

    public ActionResult GetCat()
    {
        {
            var set = new Services.SqlHelper().GetSet(@"
            SELECT CategoryType,
                   CategoryTypeName

            FROM TypeCategory
        ");

            var categoryTypeList = new List<SelectListItem>();

            if (set.Tables != null && set.Tables.Count > 0)
            {
                var categoryType = set.Tables[0];

                foreach (DataRow row in categoryType.Rows)
                {
                    categoryTypeList.Add(new SelectListItem()
                    {
                        Text = row["CategoryTypeName"].ToString(),
                        Value = row["CategoryType"].ToString()
                    });

                }

            }



            return View(new AdministratorViewModel()
            {
                CategoryType = categoryTypeList
            });

        }

    }        


    [HttpPost]
    // GET: Administrator
    public ActionResult SaveRecord(AdministratorViewModel model)
    {            
        new Services.SqlHelper().PostData(@"
            INSERT INTO Post(PostName, UserId, PostContent, CategoryType) 
            VALUES ({0}, {1}, {2}, 0)
        ", model.PostModel.PostName, model.PostModel.UserId, 
     model.PostModel.PostContent, model.PostModel.CategoryType);

        return RedirectToAction("Index");
      }
   }
 }

<  View >

  @model AdministratorViewModel


 @{
 ViewBag.Title = "Index";
 }

 <link href="~/Content/bootstrap.min.css" rel="stylesheet" />

 <div class="container" style="width:80%; margin-top:2%">

 @using (Html.BeginForm("SaveRecord", "GetCategory", "Administrator", 
 FormMethod.Post))
 {

    @Html.DropDownListFor(model => model.PostModel.UserId, Model.Users, "-- 
  select--", new { @class = "form-control" })

    @Html.DropDownListFor(model => model.PostModel.CategoryType, 
  Model.CategoryType, "--select--", new { @class = "form-control" }) 

    @Html.TextBoxFor(model => model.PostModel.PostName, new { @class = 
  "form-control", @placeholder = "Post Name" })

  @* <div class="checkbox">
    <label>
        @Html.CheckBoxFor(m => m.Remember)
        CheckBox
    </label>

  </div>*@


    <div class="TinyEditor">
        @Html.TextAreaFor(model => model.PostModel.PostContent, new { @class 
 = "form-control", @placeholder = "Post Content" })
    </div>

    <input type="submit" value="Submit" class="btn btn-block btn-primary" />

    <div>
        Posted text:
        @Html.DisplayFor(model => model.HtmlContent)
    </div>
   }



  @section Scripts
  {
    <script src="~/Scripts/tinymce/tinymce.js"></script>

    <script src="~/Scripts/main.js"></script>
  }
   </div>


  <ViewModel>
   public class AdministratorViewModel
  {

    [AllowHtml]
    public PostModel PostModel               { get; set; }
    public List<SelectListItem> Users        { get; set; }
    public List<SelectListItem> CategoryType { get; set; }
    [AllowHtml]
    public string HtmlContent { get; set; }
  }

  public class TinyEditor
  {
    [AllowHtml]
    public string HtmlContent { get; set; }

    public TinyEditor()
    {

    }
  }
  }

0 个答案:

没有答案