Good day.
I have the following model:
public class Employee { public int Id { get; set; } [Display(Name ="Информация")] public virtual ManInfo ManInfo { get; set; } [Display(Name = "Должность")] public string Post { get; set; } [Display(Name = "Рабочий отдел")] public virtual Departament Departament { get; set; } } public class Departament { public int Id { get; set; } [Display(Name = "Название отдела")] public string Name { get; set; } [Display(Name = "Цели отдела")] public string[] Aims { get; set; } [Display(Name = "Сотрудники")] public virtual ICollection<Employee> Employee { get; set; } [Display(Name = "Организация", Description = "Организация, в состав которой входит отдел")] public virtual Organization Organization { get; set; } } I'm trying to call PartialView , where the creation of this model takes place and pass through the ViewBag the Departament collection from the database.
// .cs // [HttpGet] public ActionResult Create() { ViewBag.Departaments = db.Departaments.ToList(); return PartialView("_CreateEmp"); } // .cshtml // @model Models.Employee @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Добавьте работника в каталог:</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.ManInfo.FirstName, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ManInfo.FirstName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ManInfo.FirstName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.ManInfo.SecondName, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ManInfo.SecondName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ManInfo.SecondName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.ManInfo.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ManInfo.MiddleName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ManInfo.MiddleName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.ManInfo.BirthDay, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ManInfo.BirthDay, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ManInfo.BirthDay, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <text>Определить в отдел:</text> @foreach (Models.Departament dep in ViewBag.Departaments) { @* Вот здесь у меня возникает проблема *@ } </div> <div class="form-group"> @Html.LabelFor(model => model.Post, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Post, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Post, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } I need to display a list of available Departament in the form of RadioButton in such a way that it was possible to select only one.
Well, if it is possible, it is desirable that this be “ingrained” normally in the model being created (that is, in order not to have too much code in the httpost method that the Employee model accepts)
Dropdownlistfor- Ruslan_K