1

I try to implement new validationrules in my MVC-Project but i wont become a validation-errormsg in my view. What is wrong with the code? I have googlet alot and thought i have done all right -.-

WebConfig:

<appSettings>
  <add key="ClientValidationEnabled" value="true"/> 
  <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings>

Model:

public class Model1 : IValidatableObject
{
    [Required]
    [DisplayFormat(DataFormatString="{d:0}", ApplyFormatInEditMode=true)]
    public DateTime Wunschtermin { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Wunschtermin < DateTime.Now) yield return new ValidationResult("error1!", new[] { "Wunschtermin" });
        if (Wunschtermin.Date > DateTime.Today.AddYears(2)) yield return new ValidationResult("error2", new[] { "Wunschtermin" });
    }
    ...

View:

@model TestMvcApplication.Models.Model1

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
    ...
        <div class="editor-field">
            @Html.EditorFor(model => model.Wunschtermin)
            @Html.ValidationMessageFor(model => model.Wunschtermin)
        </div>
    ...

Edit:

Maybe I can override the RangeAttribute and avoid the problem:

[Range (DateTime.Now, DateTime.Today.AddYears(2))]

usefull?

1 Answer 1

1

have solved it myself, the code in the Controler was wrong:

    [HttpPost]
    public ActionResult Create(FormCollection collection)

with a smal change:

    [HttpPost]
    public ActionResult Create(Model1 collection)

it works well :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.