Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am writing MVC listing page which need to bundle with dropdownlist.
As I am very junior to ASP.net MVC, i don't know how to make dropdownlist to run correctly and make selected dynamically.

I have two model classes

public class CycleType
{
    public int CycleTypeID { get; set; }
    public string Type { get; set; }

    public List<CycleModel> CycleModels { get; set; }
}

-----------------------------------------------------------   

public class CycleModel
{
    public int CycleModelID { get; set; }
    public int CycleTypeID { get; set; }
    public string Model { get; set; }

    public virtual CycleType CycleType { get; set; }
}

Then one Controller class,

public class CycleModelController : Controller
{
UnitOfWork<CycleModel> unitOfWork = new UnitOfWork<CycleModel>();
UnitOfWork<CycleType> unitOfWork_cycleType = new UnitOfWork<CycleType>();

...

[HttpGet]
public ActionResult Edit(int CycleModelID)
{
    CycleModel cycleModel = unitOfWork.GenericTEntityRepository.GetByID(CycleModelID);
    ViewBag.CycleType = new SelectList(unitOfWork_cycleType.GenericTEntityRepository.Get(orderBy: CycleTypes => CycleTypes.OrderBy(CycleType => CycleType.Type)));
    return View(cycleModel);
}

...
}

Then One Razor file,

<div class="editor-field">
@*Html.DropDownList("CycleType")*@
@*Html.EditorFor(model => model.CycleTypeID)*@

@Html.DropDownListFor(model => model.CycleTypeID,
                                new SelectList(ViewBag.CycleType, "Type", "CycleTypeID"))
@Html.ValidationMessageFor(model => model.CycleTypeID)
</div>

When I run my program, I get error message

DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'Type'.

1)How could I make correct this code ?
2)How could I make select item dynamically ?

Every suggestion will be really appreciated.

 

share|improve this question

2 Answers

up vote 1 down vote accepted

The ViewBag.CycleType is already a SelectList. Hence you can use that directly.

@Html.DropDownListFor(model => model.CycleTypeID, (SelectList)ViewBag.CycleType)

You can change the controller code as follows.

ViewBag.CycleType = new SelectList(
      unitOfWork_cycleType.GenericTEntityRepository.Get(
      orderBy: CycleTypes => CycleTypes.OrderBy(CycleType => CycleType.Type)), 
      "Type", "CycleTypeID");
share|improve this answer
Thank you so much for your very helpful answer @Eranga. – Frank Myat Thu Jan 10 '12 at 9:43

Take a look at my two samples.

  1. Cascading DropDownList in ASP.Net MVC
  2. Using the DropDownList and ListBox with ASP.NET MVC.

The 2nd sample will have a full tutorial published in about a week. Shoot me an email and I'll send you the tutorial. rick.anderson at microsoft.com

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.