Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I search a lot here and find something like this to make dropdown list

this is my controller:
This pass my data for dropDownList...

C#
public ActionResult Create()
  {
      var dba = new WHFMDBContext();
      var query = dba.Categories.Select(c => new { c.Id, c.Name });
      ViewBag.Id = new SelectList(query.AsEnumerable(), "Id", "Name", 3);
      return View();
  }


C#
[HttpPost]
        [InitializeSimpleMembership]
        public ActionResult Create(Profits profits)
        {
            var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
            var profit = new Profits
            {
               Value= profits.Value,
               Description = profits.Description,
               DateInput =profits.DateInput,
               CategoryName =profits.CategoryName,// ???
                User = user,

            };
            db.Profits.Add(profit);
            db.SaveChanges();
            return RedirectToAction("Index");
        }



My View :
HTML
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Profits</legend>
            
        <div class="editor-field">
           @Html.DropDownList("Id", (SelectList) ViewBag.Id, "--Select One--") 
            </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CategoryName.Id)
            @Html.ValidationMessageFor(model => model.CategoryName.Id)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Value)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Value)
            @Html.ValidationMessageFor(model => model.Value)
        </div>


This insert data to database but CategoryName_Id is NULL what am I missing ?
and CategoryName =profits.CategoryName this is a foreign key to Categories in Profits `public Categories CategoryName { get; set; }`
Posted
Comments
Jameel VM 6-Mar-13 0:51am    
Please post the Profit class code also
Ahamed Azeem 6-Mar-13 3:11am    
check the profits table included to the category table or if yes update your model file and rebuild it

1 solution

Looks to me like the problem is with the editor not the drop down. the drop down is setting Profits.Id, the editor should be setting the Profits.CategoryName.Id to whatever the user entered on the text, maybe it fails because you are not sending a model to the view.

Try sending an empty Profits to the model:
public ActionResult Create()
  {
      var dba = new WHFMDBContext();
      var query = dba.Categories.Select(c => new { c.Id, c.Name });
      ViewBag.Id = new SelectList(query.AsEnumerable(), "Id", "Name", 3);
      Profits model = new Profits();
      model.CategoryName = new CategoryName();
      return View(model);
  }


Also should'nt you have the dropdown for the category name id?
C#
@Html.DropDownListFor(model => model.CategoryName.Id,(SelectList) ViewBag.Id, "--Select One--" )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900