0

Why will not fill View page of this controller method

public ActionResult Person()
    {
        testEntities6 testPersons = new testEntities6();
        IQueryable<person> persons;

        DropDownLst.Items.Clear();
        DropDownLst.Items.Add("proba");

        persons = from i in testPersons.person
                  select i;
        return View(persons);
    }

and include namespaces:

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Mvc.Ajax;
  using MvcKVteam.Models;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;

In view page I put this code:

  <asp:DropDownList ID="DropDownLst" runat="server">
  </asp:DropDownList>

1 Answer 1

1

I don't think you can use server controls in MVC. You should be using HTML Helpers instead.

Based on your existing code:

<%= Html.DropDownList("DropDownLst", Model.AsEnumerable()) %>

But I would recommend using a View Model instead of just passing an IQueryable to the View. I also recommend you pick up an MVC book. Professional ASP.NET MVC 1.0 is really good, but doesn't cover the 2.0 release.

Rich

Sign up to request clarification or add additional context in comments.

1 Comment

I have error; There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'DropDownLst'. after using your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.