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")
}