-1

I want to Check whether the master type already exists or not using remote validation.

In my case the remote validation method is not firing.Can anyone help me?

Model

  [Key, Column(Order = 1)]
  [StringLength(200)]
  [Display(Name = "MasterType")]
  [Remote("IsNameAvailable", "MasterSetUps", ErrorMessage = "Master type already exists ")]
  public string MasterType { get; set; }

Validation Method

    [AllowAnonymous]
    public JsonResult IsNameAvailable(string MasterType)
    {
        bool result = true;
        if (s_mode == "ADD")
        {
            return Json(!db.MasterSetUps.Any(a => a.MasterType == MasterType), JsonRequestBehavior.AllowGet);
        }
        else if (s_mode == "EDIT" & MasterType != s_Master_Type_name)
        {
            return Json(!db.MasterSetUps.Any(a => a.MasterType == MasterType), JsonRequestBehavior.AllowGet);
        }
        return Json(result, JsonRequestBehavior.AllowGet);
    }

View

<div class="form-group">
     @Html.LabelFor(model => model.MasterType, htmlAttributes: new { @class = "control-label col-sm-2" })
   <div class="col-sm-10">
     @Html.DropDownList("MasterType", null, htmlAttributes: new { @class = "form-controls" })
     @Html.ValidationMessageFor(model => model.MasterType, "", new { @class = "text-danger" })
   </div>
</div>

GetMethod

public ActionResult Create()
{
  s_mode = "ADD";
  ViewBag.MasterType = new SelectList(db.Masters, "MasterType", "MasterType");
  return View();
}
Reshma
  • 436
  • 3
  • 13
  • 1
    Your property is `string` so your view code for the `DropDownList` makes no sense (and certainly will not generate any `data-val` attributes necessary for validation). Show the code for your GET method –  Jul 11 '17 at 07:07
  • 1
    Why not using `DropDownListFor` while `ValidationMessageFor` exists: `@Html.DropDownListFor(model => model.MasterType, ViewBag.MasterType as SelectList, new { @class = "control-label col-sm-2" })`? – Tetsuya Yamamoto Jul 11 '17 at 07:22
  • @TetsuyaYamamoto Thanks for your sugession..Its working :) Thank You. – Reshma Jul 11 '17 at 07:28
  • @StephenMuecke Thank you for your effort.Got an alternative method :) – Reshma Jul 11 '17 at 07:29
  • That will not work correctly! –  Jul 11 '17 at 07:37
  • Refer [this answer](https://stackoverflow.com/questions/37161202/can-the-viewbag-name-be-the-same-as-the-model-property-name-in-a-dropdownlist/37162557#37162557) to understand why –  Jul 11 '17 at 07:41

1 Answers1

1

Your use of DropDownList(...) means that your not generating the necessary data-val-* attributes for validation. The method is using your ViewBag property for binding (not your model property) and their are no validation attributes associated with ViewBag.

Change the code in the GET method to

ViewBag.MasterTypeList = new SelectList (.....

and the view code to

@Html.DropDownListFor (m => m.MasterType, (SelectList)ViewBag.MasterTypeList, new { ... })

Note that the name of the property your binding to cannot be the same as the SelectList.

  • hi there, how to bind `selectedValue` from controller? – Jun Rikson Aug 23 '18 at 17:38
  • @ColourDalnet, If the vale of the property you are binding to (`MasterType` in this case) matches one of the option values (in `MasterTypeList`), then the correct option will be selected. –  Aug 23 '18 at 22:55
  • I thought so, but... when I bound like OP question, it is bound perfectly but Validation not working properly. Doing in your way making validation working perfectly but, binding the data not working. I will iterate the code once again and ask new question if not working. Thank you. – Jun Rikson Aug 24 '18 at 05:55