Hello, I am working with ASP.NET trying to make a new object of categories from other classes and as I am passing all relevant parameters of both classes. But apparently I'm missing something I do not know if it's because of what I'm going through or if I'm definitely not having a good time.
//CategoriesController
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public async Task < ActionResult > Create([Bind(Include = "Id,Name,Description")] Categories category) {
if (ModelState.IsValid) {
db.Category.Add(category);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(category);
}
//model
using System.ComponentModel.DataAnnotations;
namespace PCotiza_compras.Models {
public class Categories {
public int Id {
get;
set;
}
[Display(Name = "Nombre")]
public string Name {
get;
set;
}
[Display(Name = "Descripción")]
public string Description {
get;
set;
}
}
}
//model CategoriesDepartmentViewModel
namespace PCotiza_compras.Models {
public class CategoriesDepartmentViewModel {
[Display(Name = "Departamentos")]
public List < Departments > DepartmentItem {
get;
set;
}
[Display(Name = "Categorias")]
public Categories CategoriesItem {
get;
set;
}
}
}
@*@model PCotiza_compras.Models.Categories*@ @model PCotiza_compras.Models.CategoriesDepartmentViewModel @using (Html.BeginForm("Create", "Categories", FormMethod.Post, new { Id = "modalCategories" })) {
<div class="modal-body" style="overflow:hidden;">
@Html.AntiForgeryToken() @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @* Select del departamento *@
<div class="form-group">
@Html.LabelFor(model => model.DepartmentItem, htmlAttributes: new { @class = "control-label col-md-10" })
<div class="col-md-12">
<select class="form-control" id="Departamento" name="Departamento">
@foreach (var d in Model.DepartmentItem)
{
<option value="@d.Id">@d.Code - @d.Description</option>
}
</select>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CategoriesItem.Name, htmlAttributes: new { @class = "control-label col-md-10" })
<div class="col-md-12">
@Html.EditorFor(model => model.CategoriesItem.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.CategoriesItem.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CategoriesItem.Description, htmlAttributes: new { @class = "control-label col-md-10" })
<div class="col-md-12">
@Html.EditorFor(model => model.CategoriesItem.Description, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.CategoriesItem.Description, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancelar</button>
<input id="buttonNewCategory" type="submit" value="Crear" class="btn btn-success" />
</div>
}