Afternoon Folks,

Im new to MVC 5 and C# and have a simple form with several fields and a dropdown box. I have used the CRUD method for entity framework and can successfully view and create new records in the system.

The only issue that I have is I have a 'Title' dropdown that links to the entity framework and populates these titles into a list. when I load my web page I can now see the titles available in the dropdown list, but when submitting the form, all the values but the 'Title' field are submitted into the database.

When I debug my program, this field regardless of what I select displays null.

I have followed the following tutorial to get this wo work and looked around the net but im struggeling to find a solution.

Link to tutorial

I have two tables in my model, one named 'Title' and the other named 'Client Record'.

As I have used the database first approach and not code first I have combined these two database models into one:

namespace EDT_Test.Models.ViewModels
   {
   public partial class Marie_Testing
   {
    [Display(Name = "Client Ref:")]
    public int id { get; set; }

    [Display(Name = "Created By:")]
    public string CreatedBy { get; set; }

    public List<Title> allTitles { get; set; }

    [Required]
    [Display(Name = "Surname:")]
    public string Surname { get; set; }

    [Display(Name = "Additional Surname:")]
    public string Surname2 { get; set; }

    [Required]
    [Display(Name = "Forename:")]
    public string Forename1 { get; set; }

    [Display(Name = "Additional Forename:")]
    public string Forename2 { get; set; }

The generated Entity Framework model looks like this:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace EDT_Test.Models
{
    using System;
    using System.Collections.Generic;

    public partial class ClientRecord
    {
        public int id { get; set; }
        public string CreatedBy { get; set; }
        public string Title { get; set; }
        public string Surname { get; set; }
        public string Surname2 { get; set; }
        public string Forename1 { get; set; }
        public string Forename2 { get; set; }
    }
}

The only difference for the Title field between the auto created model and Marie_Testing model is I have changed the Title field from a string to a list item.

My Create.cshtml holds a div for the Title dropdown that looks like this (This links to my model named Marie_Testing and not the auto generated ones created by the Entity Framework:

<div class="form-group">
    @Html.LabelFor(model => model.allTitles, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <select id="titleid" name="titlename" class="form-control">
            @foreach (var item in Model.allTitles)
            {
                <option value="@item.id">@item.Title1</option>
            }
        </select>
    </div>
</div>

My code for the ClientRecordsController is:

 // GET: ClientRecords/Create
    public ActionResult Create()
    {
        ////set the defaults (dropdown) of the page for the ceaton of a new record.
        Marie_Testing vmPopulateData = new Marie_Testing();

        List<Title> titles = (from t in db.Titles select t).ToList();
        //List<Title> titles = Title.Select(t => new{t.id, t.Title}.ToString.ToList());
        vmPopulateData.allTitles = titles;

        return View(vmPopulateData);
    }

    // POST: ClientRecords/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "id,CreatedBy,Title,Surname,Surname2,Forename1,Forename2")] ClientRecord clientRecord)
    {
        if (ModelState.IsValid)
        {
            db.ClientRecords.Add(clientRecord);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(clientRecord);
    }

Any help is much appreciated as I don't understand how can see the dropdown list in the web page but cannot seem to grab the selected value and post this to the database.

Regards Betty B

Related posts

Recent Viewed