I am new to MVC and web development and I am doing some exercise work given to me. The whole thing is quite simple really, but for some reason I cant make the validation StringLength()
and Required
validation work in the whole project. I dealt with it, checking the views and webconfig files until I simply gave up and made a new project all together.
I did practically the same thing, but for some reason the Required
and StringLength
validation worked fine on a part of my project but did not work on an another. So I was left again with a partial working project, only this time the part that worked in the first project stopped working in the other.
I added all the scripts I needed: the jquery validation and the unobstrusive
The following is a class I made to add the validations :
[MetadataType(typeof (PuestoMetaData))]
public partial class Puesto
{
}
public class PuestoMetaData
{
[Required(ErrorMessage = "El campo es requerido")]
[StringLength(30, MinimumLength = 3, ErrorMessage = "El campo tiene que tener por lo minimo 3 caracteres y maximo 30 caracteres")]
public string Descripcion { get; set; }
}
Its controller:
// GET: Puesto/Create
public ActionResult Create()
{
return View();
}
// POST: Puesto/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Descripcion")] Puesto puesto)
{
if (ModelState.IsValid)
{
db.Puesto.Add(puesto);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(puesto);
}
Its view:
@model coello_roy_Act3.Contexto.Puesto
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Puesto</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Descripcion, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Descripcion, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Descripcion, "", 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 dont know if it is needed, but here is the class created by the entity framework
namespace coello_roy_Act3.Contexto
{
using System;
using System.Collections.Generic;
public partial class Puesto
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Puesto()
{
this.Empleado = new HashSet<Empleado>();
}
public int Id { get; set; }
public string Descripcion { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Empleado> Empleado { get; set; }
}
}