my view
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
@model Riski.Models.Koef
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Model.name
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.koef1, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.koef1, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.koef1, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.id)
@Html.HiddenFor(model => model.name)
}
@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 steps1 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";
No comments:
Post a Comment
Note: only a member of this blog may post a comment.