locked
Drop Down list in MVC Razor... Anyone have any Idea why the selected value not saving to database???? RRS feed

  • Question

  • User2130491911 posted

    My bane in MVC are Viewmodels (With some guidance I have a little better understanding today then I did  yesterday) and bloddy Drop Down List. They are so hard for me to wrap my head around them.

    My issue today is that I cannot get the value that gets selected to save to the database... what on earth could be the issue????

    My source codes. I will show the view model the Controller and the razor form view

    Maybe someone can see the disconnect.

    This is the so called view model where the select list was added

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    
    namespace CMS.ViewModelsClasses
    {
        public class SC_Log_ViewModel
    
        {
            public int SC_REC_ID { get; set; }
    
            [Display(Name = "JUDGE ID")]
            public string SC_JUDGE_ID { get; set; }
    
            //[Display(Name = "CONTESTANT ID")]
            //public Nullable<int> SC_CONTESTANT_ID { get; set; }
    
            [Display(Name = "CONTESTANT ID")]
            public string SC_CONTESTANT_ID { get; set; }
    
    
            [Display(Name = "CONTEST ID")]
            public Nullable<int> SC_CONTEST_ID { get; set; }
    
            [Display(Name = "TYPE RECIEVED")]
            public Nullable<int> SC_SCORE_TYPE { get; set; }
    
            [Display(Name = "SCORE")]
            public Nullable<int> SC_SCORE { get; set; }
    
            [Display(Name = "DATE RECIEVED TEXT")]
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
            public Nullable<System.DateTime> SC_SCORE_ENTRY_DATE { get; set; }
    
            [Display(Name = "SPECIAL SCORECARE ID")]
            public Nullable<int> SC_SCORE_CARD_ID { get; set; }
    
            [Display(Name = "AUDIT DATE")]
            public Nullable<System.DateTime> SC_AUDIT_DATE { get; set; }
    
    
    
            //FOr the DDL For Contest
            //See: https://stackoverflow.com/questions/18613851/asp-net-mvc-dropdown-list-from-database
            public List<SelectList> CONTEST_LIST_DDL { get; set; }
        }
    
    }

    Here are the two controller elements for the Create Option

            // GET: SC/Create
            public ActionResult SCCreate()
            {
                var Contest_List = db.CT_CONTEST_MASTER_TBL.ToList();
    
                if (Contest_List != null)
                {
                    ViewBag.ContestList = Contest_List;
                }
    
    
                return View();
            }
    
            // POST: SC/Create
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
    
    
    
            public ActionResult SCCreate([Bind(Include = "SC_REC_ID,SC_JUDGE_ID,SC_CONTESTANT_ID,CONTEST_ID,SC_SCORE_TYPE,SC_SCORE,SC_SCORE_ENTRY_DATE,SCORE_CARD_ID,SC_AUDIT_DATE,CONTEST_LIST_DDL")] SC_SCORECARD_TBL sC_SCORECARD_TBL)
            {
                if (ModelState.IsValid)
                {
    
                    db.SC_SCORECARD_TBL.Add(sC_SCORECARD_TBL);
                    db.SaveChanges();
                    return RedirectToAction("SCSpecialScores");
    
                }
    
                return View(sC_SCORECARD_TBL);
            }

    Here are bits of the razor form cshtml

    @model CMS.ViewModelsClasses.SC_Log_ViewModel
    
    
    
    
            <div class="form-group">
                @Html.LabelFor(model => model.SC_CONTEST_ID, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.SC_CONTEST_ID, new SelectList(ViewBag.ContestList, "CT_CONTEST_ID", "CT_CONTEST_ID"), "PLEASE SELECT A CONTEST"))
    
                    @*new { htmlAttributes = new { @class = "form-control", @onchange = "ValidateContest()" } }))*@
    
                    @*<input id="SC_CONTEST_ID" name="item.Id" />*@ @*type="hidden"*@
                    @*@Html.EditorFor(model => model.SC_CONTEST_ID, new { htmlAttributes = new { @class = "form-control", @onchange = "ValidateContest()" } })*@
                    @Html.ValidationMessageFor(model => model.SC_CONTEST_ID, "", new { @class = "text-danger" })
                </div>
            </div>

    When I step through it, the SC_CONTEST_ID comes back from razor Null!?

    Wednesday, October 7, 2020 7:05 PM

Answers

  • User475983607 posted

    The SCCreate action does not include the "SC_CONTEST_ID" parameter in the Bind list.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2020 7:32 PM
  • User-474980206 posted

    the form is bound to the class SC_Log_ViewModel, and the action is bound to a different class SC_SCORECARD_TABLE. The action binding, which you explicitly state, does not include the post back value SC_CONTEST_ID. 

    I assume the names differ between the view model and the entity model. unless you write a custom binder, you should change the action to bind to the view model, then in code in the action, create a new entity model, and copy values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2020 7:36 PM
  • User1312693872 posted

    Hi,AppDev01

    When I step through it, the SC_CONTEST_ID comes back from razor Null!?

    [Bind] attribute specifies which properties of a model should be included in model binding.

    You should add SC_CONTEST_ID in it, like:

    public ActionResult Index([Bind(Include = "SC_REC_ID,SC_JUDGE_ID,SC_CONTESTANT_ID,CONTEST_ID,SC_SCORE_TYPE,SC_CONTEST_ID,SC_SCORE,SC_SCORE_ENTRY_DATE,SCORE_CARD_ID,SC_AUDIT_DATE,CONTEST_LIST_DDL")]

    Result in my demo:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 2:23 AM

All replies

  • User475983607 posted

    The SCCreate action does not include the "SC_CONTEST_ID" parameter in the Bind list.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2020 7:32 PM
  • User-474980206 posted

    the form is bound to the class SC_Log_ViewModel, and the action is bound to a different class SC_SCORECARD_TABLE. The action binding, which you explicitly state, does not include the post back value SC_CONTEST_ID. 

    I assume the names differ between the view model and the entity model. unless you write a custom binder, you should change the action to bind to the view model, then in code in the action, create a new entity model, and copy values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2020 7:36 PM
  • User1312693872 posted

    Hi,AppDev01

    When I step through it, the SC_CONTEST_ID comes back from razor Null!?

    [Bind] attribute specifies which properties of a model should be included in model binding.

    You should add SC_CONTEST_ID in it, like:

    public ActionResult Index([Bind(Include = "SC_REC_ID,SC_JUDGE_ID,SC_CONTESTANT_ID,CONTEST_ID,SC_SCORE_TYPE,SC_CONTEST_ID,SC_SCORE,SC_SCORE_ENTRY_DATE,SCORE_CARD_ID,SC_AUDIT_DATE,CONTEST_LIST_DDL")]

    Result in my demo:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 2:23 AM