Can not insert the value NULL into column 'Name'

0

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>
}
    
asked by E.Rawrdríguez.Ophanim 29.11.2017 в 18:45
source

1 answer

1

In your post, in the action method (Action Method) you want to receive an element of the type Categories, but your model is of the type CategoriesDepartmentViewModel. Your html form is sending element with id CategoriesItem_Name.

Basically you need to change the type of object you are receiving:

[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Description")] CategoriesDepartmentViewModel categoriesDepartmentViewModel)
{
    Categories category = categoriesDepartmentViewModel.CategoriesItem != null ? categoriesDepartmentViewModel.CategoriesItem : new Categories();
    if (ModelState.IsValid)
    {
        db.Category.Add(category);
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }

    return View(category);
}

PD1: The example gets the category in a if of a single line, this is optional and you can modify it. The important thing in the answer is the type of object expected in the action method. PD2: Beware of the model that you are going back to your sight. As I understand it has to be of the CategoriesDepartmentViewModel type

    
answered by 29.11.2017 в 22:17