my view
@model Riski.Models.Koef
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
}
@Html.ActionLink("Вернуться к списку", "Index")
@section scripts{
@Scripts.Render("~/bundles/jqueryval")
}
my controller
[HttpPost]
public ActionResult Edit(Koef koef)
{
if (ModelState.IsValid)
{
db.Configuration.ValidateOnSaveEnabled = false;
db.Koefs.Attach(koef);
var entry = db.Entry(koef);
entry.Property(e => e.koef1).IsModified = true;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(koef);
}
this is a partial class wich helps us to validate our model. Acording to this approach we don't need to add Validation Attributies after every process of generation Entity classes
using System;
using System.ComponentModel.DataAnnotations;
namespace Riski.Models
{
[MetadataType(typeof(KoefViewModel))]
public partial class Koef
{
}
public class KoefViewModel
{
[Required]
[Display(Name = "Коэффициент")]
[Range(0, 100, ErrorMessage = "Значение должно быть от 0 до 100.")]
public Nullable koef1 { get; set; }
[Required]
public int id { get; set; }
}
}
That's it.
Now , as this is a russian web-site, we need to override some standard validation messages. It's simple solution and we should do only for steps
1 Add folder
2 Create resource file
3 Add new value (We can override 4 types of messages
FieldMustBeDate, FieldMustBeNumeric, PropertyValueInvalid andPropertyValueRequired)
4 And add one line to your Global.asax.cs
DefaultModelBinder.ResourceClassKey = "MyNewResource";