Click here to Skip to main content
11,490,003 members (50,254 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MVC
I search a lot here and find something like this to make dropdown list

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

  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();
    }

    [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 :
@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 5-Mar-13 8:47am
krasi89352
Comments
Jameel Moideen at 6-Mar-13 0:51am
   
Please post the Profit class code also
Ahamed Azeem at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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?
@Html.DropDownListFor(model => model.CategoryName.Id,(SelectList) ViewBag.Id, "--Select One--" )
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 650
1 Andy Lanng 233
2 OriginalGriff 188
3 Sascha Lefèvre 170
4 Maciej Los 160
0 Sergey Alexandrovich Kryukov 9,713
1 OriginalGriff 8,475
2 Sascha Lefèvre 3,504
3 Maciej Los 3,106
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100