NPG_Chemical_Measurement_Methods
is an ICollection
type. In my Chemical.cshtml
, I have:
<div id="nioshs">
@Html.EditorFor(model => model.NPG_Chemical_Measurement_Methods)
</div>
and in the EditorTemplate
view:
<div class="method" style="display:inline-block;">
<p>
@Html.RemoveLink("x", "div.method", "input.mark-for-delete")
@Html.HiddenFor(x => x.DeleteMethod, new { @class = "mark-for-delete" })
@Html.TextBoxFor(x => x.Measurement_Method)
@Html.ValidationMessageFor(model => model.Measurement_Method, "", new { @class = "text-danger" })
@Html.Hidden("Measurement_Type", "NIOSH")
</p>
</div>
I want to have something like when I give input for @Html.TextBoxFor(x => x.Measurement_Method)
, then click on other place of the current page, an alert will popup says Not exist in Database
if record cannot be found in Measurement_Method
table.
NPG_Chemical.cs has:
public partial class NPG_Chemical
{
public NPG_Chemical()
{
this.NPG_Chemical_Measurement_Methods = new HashSet<NPG_Chemical_Measurement_Method>();
}
[StringLength(256)]
[Remote("IsUserExists", "NPG_Chemical", ErrorMessage = "Chemical Name already in use")]
public string Chemical { get; set; }
public virtual ICollection<NPG_Chemical_Measurement_Method> NPG_Chemical_Measurement_Methods { get; set; }
internal void CreateMeasurementMethods(int count = 1)
{
for (int i = 0; i < count; i++)
{
NPG_Chemical_Measurement_Methods.Add(new NPG_Chemical_Measurement_Method());
}
}
Measurement_Method.cs has:
public partial class NPG_Chemical_Measurement_Method
{
[StringLength(256)]
[Remote("IsNIOSHExists", "NPG_Chemical", ErrorMessage = "NIOSH number does not exist")]
public string Measurement_Method { get; set; }
}
NPG_ChemicalController has:
public JsonResult IsUserExists(string Chemical)
{
return Json(!db.NPG_Chemical.Any(x => x.Chemical == Chemical), JsonRequestBehavior.AllowGet);
}
public JsonResult IsNIOSHExists(string Measurement_Method)
{
System.Diagnostics.Debug.WriteLine("value:",Measurement_Method);
return Json(db.NPG_NIOSH_Method.Any(x => x.Measurement_Number == Measurement_Method), JsonRequestBehavior.AllowGet);
}