onedevteam.com onedevteam.com - 5 months ago 222
Javascript Question

Remote validation not working for partial view in asp.net mvc 5

I have an editor field declared in razor partial view as

<div class="form-group">
@Html.LabelFor(model => model.sifra_materijala, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.sifra_materijala)
@Html.ValidationMessageFor(model => model.sifra_materijala)
</div>
</div>


View is called with "RenderPartial" from index.

This generate's html:

<input type="text" value="" name="sifra_materijala" id="sifra_materijala"
data-val-remote-url="/NormativiMaterijala/MaterijalUPoziciji"
data-val-remote-additionalfields="*.sifra_materijala"
data-val-remote="'sifra_materijala' is invalid."
data-val="true" class="text-box single-line">


I also have

[HttpGet]
public virtual JsonResult MaterijalUPoziciji(string sifra_materijala)
{
// do some checking and return json result..
}


and

[Remote("MaterijalUPoziciji", "NormativiMaterijala", ErrorMessage = "Already Exists")]
public string sifra_materijala { get; set; }


in model that need to be checked...
All this just don't work for no obvious reason. I checked spelling, jquery unobtrusive scripts and everything is ok, but validation method is never called.

For things to be even more confusing, i have all this on another page working perfectly fine...

Can someone help me with this?

EDIT: Checking page in firebug show that no request is send after exiting/changing editbox with data that need to be validated...

I also have scripts included.

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


I also added second validation, that is not remote and this one is working but remote is still dead...

[StringLength(10, MinimumLength = 5, ErrorMessage = "Sifra must be minimum 5 characters long")]
[Remote("MaterijalUPoziciji", "NormativiMaterijala", ErrorMessage = "Already Exists")]
public string sifra_materijala { get; set; }

Answer

Solved it after sleepless night of trying and error... problem was in "virtual" declaration of my controller function.. Removed that and working perfect now...

[HttpGet]
    public JsonResult MaterijalUPoziciji(string sifra_materijala)
    { 
       // do some checking and return json result..
    }

I hope this answer will help someone sometime...