1

I'm new to C# and MVC and I'm working on a homework assignment. Right now I'm stuck on the last thing to do in my project, which is a Html.DropDownListFor().

I've got a Model which gets the information I want: Newsmodel.cs

        public IQueryable<SelectListItem> returnCatagory()
    {
        return (db.groups
                  .AsQueryable()
                  .Select(s => new SelectListItem() { 
                      Value = s.ID.ToString(), 
                      Text = s.Flokkur 
                  })
               );
    }

Then a create() in my HomeController.cs

        //get
    public ActionResult Create()
    {
        NewsModel model = new NewsModel();
        model.Dagsetning = DateTime.Now;
        return View(model);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(NewsModel model)
    {

        if (ModelState.IsValid)
        {
            New news = new New();
            news.Fyrirsögn = model.Fyrirsögn;
            news.Flokkur = model.Flokkur;
            news.Frétt = model.Frett;
            news.Date = model.Dagsetning;
            model.Dagsetning = DateTime.Now;
            news.ID = model.ID;

            nr.Add(news);
            nr.Save();
            return View("Saved");
        }
        return View(model);
    }

Finally I've got the Create.aspx view

<%@ Page Title="" 
         Language="C#" 
         MasterPageFile="~/Views/Shared/Site.Master" 
         Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.NewsModel>" %>
...

            <div class="editor-label">
            <%: Html.LabelFor(model => model.Flokkur) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Flokkur) %>
            <%: Html.ValidationMessageFor(model => model.Flokkur) %>
            <%: Html.DropDownListFor(..) %>                
        </div>

I've tried about everything for the <%: Html.DropDownListFor(..) %>, but frankly I've got no idea what's missing or what has to be done so I can access it. Nerddinner did not help me much here.

Bart
  • 19,692
  • 7
  • 68
  • 77
CyberBuffalo
  • 21
  • 2
  • 3
  • what is model.Flokkur and what do you expect to show in the dropdown. – gideon Feb 26 '11 at 12:51
  • (Flokkur is Icelandic for Category) And is supposed to show all the categories from a table called groups. So the field is connected to a table called News which has the id for "Flokkur". (Hope this helps) – CyberBuffalo Feb 26 '11 at 13:00
  • Also what I want to show in the DropDown is the list of what is in Flokkur. So in the table I've got 4-5 (varchar(15)) categories for the user to select. – CyberBuffalo Feb 26 '11 at 13:05

3 Answers3

1

in your Create() method, you can do

ViewData["SelectList"] = returnCatagory().AsEnumerable();

and in the view

<%: Html.DropDownListFor(model => model.Flokkur, ViewData["SelectList"])%>

This is one way to do it. If you want things to be strongly typed, you have to create a ViewModel. This post's answer shows how you can do that.

Community
  • 1
  • 1
Bala R
  • 107,317
  • 23
  • 199
  • 210
1

If i understand correctly, you have a list of categories and want it returned in your model. First, you need to add a selectedFlokkur (or something of the sort) to your model that will receive the selected value.

public class NewsModel
...    
public int SelectedFlokkur {get; set;}

Then, try something like this in your view:

<%:Html.DropDownListFor( c => c.SelectedFlokkur, Model.returnCatagory, "-- Select Flokkur --")%>

Then, in your post, you can use the SelectedFlokkur to set the catagory.

Hope this helps.

NOTE: In the past, i've tried to use an object instead of and int for SelectedFlokkur. Howerver, MVC by desing does NOT fill in your entire object, it only sets the ID. This also caused problems with entity framework and temporary objects in the context. So, i strongly recommend using the int and retrieving only the ID.

mateuscb
  • 10,150
  • 3
  • 52
  • 76
0
  1. In your controller prepare select list item and put it in dataview

        List<SelectListItem> selectList = new List<SelectListItem>();
        foreach (var object in objectlList)
        {
            SelectListItem item = new SelectListItem();
            item.Text = object.Property;
            item.Value = object.Id.ToString();
            selectList .Add(item);
        }
        ViewData["yourViewData"] = selectList ;
    
  2. In view bind your viewdata to your list

    <%: Html.DropDownListFor(model => model.property, ViewData["yourViewData"])%>

Jatin patil
  • 4,252
  • 1
  • 18
  • 27