-2

I am beginner in asp.net so may be I have stupid question, but I need some help. I have a model and controller MAIN, with read/write rules. I have a create action in MainController, and it works, but I need that one of the field be dropdown and it has options from another table of Department. I have searched answer but I have not get full answer. So if you can, help me. If need I will write codes. Thanks a lot.

MainController

 public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Main/Create

        [HttpPost]
        public ActionResult Create(Main main)
        {
            if (ModelState.IsValid)
            {
                db.Main.Add(main);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(main);
        }

Main.cs (Model)

    namespace PhoneBook.Models
{
    public class Main
    {
        public int Id { get; set; }
        public string F_L_Name { get; set; }
        public string Regioni { get; set; }
        public string Raioni { get; set; }
        public string Department { get; set; }
        public string Division { get; set; }
        public string Position { get; set; }
        public string Mobile { get; set; }
        public string I_Phone { get; set; }
        public string Email { get; set; }
        public int Deleted { get; set; }
        public string Comment { get; set; }
        public string All_In_One { get; set; }
        public DbSet<Department> DepartmentFI { get; set; }
    }
}

Create.cshtml (View)

    @model PhoneBook.Models.Main

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Main</legend>

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

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

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

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

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

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

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

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

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

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
  • Possible duplicate of [Asp.net mvc dropdown list using ViewBag](https://stackoverflow.com/questions/46341239/asp-net-mvc-dropdown-list-using-viewbag) – Nitesh Kumar Feb 19 '18 at 10:15
  • it's not works. i get this error "There is no ViewData item of type 'IEnumerable' that has the key 'Name'." – Tsotne Lomidze Feb 19 '18 at 10:26
  • 1
    Ok well an error message is useful, but we can't fix code we can't see. Add both the error details and the relevant code into the question, please. – ADyson Feb 19 '18 at 13:54
  • To understand that error, and how to generate a dropdownlist correctly, refer [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) –  Feb 19 '18 at 23:48

1 Answers1

0

use like this to display department as drop down list

@Html.DropDownListFor(model => model.Department, model=>model.Departments)

Note: you need to populate model.Departments with the values from database table in controller/service

user9405863
  • 1,506
  • 1
  • 11
  • 16