0

In product class i have code properties which need to be unique. I use remote validation. but it makes problem when editing. so i add AdditionalFields. it works fine when perform edit operation but product can not create now. it never hit controller when use AdditionalFields in remote validation.. My model:

  public class Product
{
    public int Id { get; set; }

    [Remote("IsCodeAvailable", "Product", AdditionalFields ="Id", HttpMethod = "GET", ErrorMessage = "Product Code already taken")]
    public string Code { get; set; }


    public string Name { get; set; }

}

here is my validation code

 [HttpGet]
     public JsonResult IsCodeAvailable(string code, int Id)
    {
        try
        {
            var flag = true;

            if (Id == 0) // its a new object
            {
                flag = !db.Product.Any(x => x.Code == code);
            }
            else // its an existing object so exclude existing objects with the id
            {
                flag = !db.Product.Any(x => x.Code == code && x.Id != Id);
            }


            return Json(flag, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json(false, JsonRequestBehavior.AllowGet);
        }
    }

and here is my view

                            <div class="form-horizontal">
                                @Html.HiddenFor(model => model.Id)
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Code, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.EditorFor(model => model.Code, new { htmlAttributes = new { @class = "form-control", @required = "required", @style = "text-transform:uppercase" } })
                                        @Html.ValidationMessageFor(model => model.Code, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                                    </div>
                                </div>

Any kind of help will be greatly appreciated

Iftekhar Rasul
  • 51
  • 2
  • 10
  • Are you including the necessary hidden input for `Id` in your 'Create' view? –  May 10 '18 at 12:11
  • I edit my question.Add cshtml. @StephenMuecke – Iftekhar Rasul May 10 '18 at 12:24
  • Is that the Create view or Edit view (or both)? –  May 10 '18 at 12:29
  • And as a side note, remove the `@required = "required"` - that will not work with mvc's client side validation. If you want it to be required, add a `[Required]` attribute to the property –  May 10 '18 at 12:30
  • It is Create view and its work perfectly fine without additional fields in remote and thanks for your advice i remove @required = "required" and add require attribute to the property. – Iftekhar Rasul May 10 '18 at 12:36
  • The code you have shown works fine. Are you claiming that the `IsCodeAvailable` method is not being executed, or that the POST method is not being hit? –  May 10 '18 at 12:39
  • Both method is not being hit when create a product. I can't figure out what's wrong in this simple code. – Iftekhar Rasul May 10 '18 at 12:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170780/discussion-between-stephen-muecke-and-iftekhar-rasul). –  May 10 '18 at 12:46

0 Answers0