0

I have applied remote validation on multiple fields based on the answer of this question on year and section fields to ensure a user will not enter same section in same year but it is not show any validation message

Model (auto generated from database but I have added [Remote] and [Required])

[Required]
public Nullable<short> Year { get; set; }
[Required]
[Remote("IsSectionAvailable", "Test", AdditionalFields = "Year", ErrorMessage = "Value is not valid")]
public string Section { get; set; }

the function in Controller

[AllowAnonymous]
[HttpPost]
public ActionResult IsSectionAvailable(string Section,short Year)
{
    try
    {
        return Json(!IsSectionExists(Section,Year));
    }
    catch (Exception ex)
    {
        return Json(false);
    }
}
ITDBEntities1 db = new ITDBEntities1();

private bool IsSectionExists(string Section, short Year)
{
    // Query for all projects with section 
    var project = from s in db.SeniorProject
                where s.Section==Section
                select s;
    var Y = from s in db.SeniorProject
                  where s.Year == Year
                  select s;
    if (project.Any()&&Y.Any())
    {
        return true;
    }
    return false;
}

form in View

@model Testinggggg.Models.SeniorProject
....
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true);

    @Html.LabelFor(model => model.Section)
    @Html.EditorFor(model => model.Section)
    @Html.ValidationMessageFor(model => model.Section)

    @Html.LabelFor(model => model.Year)
    @Html.EditorFor(model => model.Year)
    @Html.ValidationMessageFor(model => model.Year)
}

The view includes

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
programmer
  • 109
  • 1
  • 7
  • Your `IsSectionExists()` method just returns true if the `Section` and `Year` exist - it does not validated that the _user will not enter same section in same year _ –  Apr 15 '18 at 00:53
  • Your query need to be `var project from s in db.SeniorProject where s.Section==Section && s.Year == Year select s; return project.Any();` –  Apr 15 '18 at 02:37
  • In addition, your form does not include a submit button (and I have deleted all the irrelevant code from your question) –  Apr 15 '18 at 02:44
  • @StephenMuecke , your form does not include a submit button (yes I didn't have any submit button until now first I would like to do remote validation correctly then I will add it because remote validation does not require submit button right? ) – programmer Apr 15 '18 at 04:25
  • @StephenMuecke and I have deleted all the irrelevant code from your question (thanks to do that but I have put complete view because the order of libraries may affect I don't know) – programmer Apr 15 '18 at 04:27
  • So long as you have not reconfigured the validator, then it will be triggered when you enter a value in `Section` and tab out (and thereafter it will be triggered on each keyup). And of course you can put a breakpoint on the method and check that –  Apr 15 '18 at 04:28
  • yes I have tried that many time but It is not show any message – programmer Apr 15 '18 at 04:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169010/discussion-between-stephen-muecke-and-programmer). –  Apr 15 '18 at 04:31

0 Answers0