Click here to Skip to main content
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
krasi89340
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 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2014
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