1

I have a problem with my Remote Validation.

The following is my model :

[MetadataType(typeof(M_ToolingAnnotation))]
public partial class M_Tooling
{ 
    public string ToolingID { get; set; }
}

internal sealed class M_ToolingAnnotation
{
    [Required]
    [Display(Name = "Tooling ID")]
    [StringLength(50, ErrorMessage = "The {0} must be less than 50 characters long.")]
    [Remote("CheckToolingID", "Tooling2", AdditionalFields = "ToolingID_Ori", ErrorMessage = "Tooling ID already in use!")]  
    public string ToolingID { get; set; }
}

public class M_ToolingViewModels2 : M_Tooling
{
    public M_ToolingViewModels2()
    { 
        this.M_Tooling = new M_Tooling();
    }

    public M_Tooling M_Tooling { get; set; }

    public string LocationID { get; set; } 
}

The following is the controller :

public ActionResult Index()
{ 
    ViewBag.ToolingID_Ori = "lalala";

    return View();
}

[HttpGet]
public JsonResult CheckToolingID([Bind(Prefix = "M_Tooling.ToolingID")] string ToolingID, string ToolingID_Ori )
{
    var result = true;

    if (ToolingID != ToolingID_Ori)
    {
        var routingID = db.M_Tooling.Where(u => u.ToolingID == ToolingID).FirstOrDefault();

        if (routingID != null)
        {
            result = false;
            ModelState.AddModelError(string.Empty, "Tooling ID already exists.");
        }
    }

    return Json(result, JsonRequestBehavior.AllowGet);
}

Finally the view:

@Html.Hidden("ToolingID_Ori", (string)ViewBag.ToolingID_Ori);
@Html.LabelFor(model => model.M_Tooling.ToolingID, "Tooling ID*", htmlAttributes: new { @class = "col-md-2 control-label", @style = "color:red" })
@Html.TextBoxFor(model => model.M_Tooling.ToolingID, new { @class = "col-md-2 form-control", @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.M_Tooling.ToolingID, "", new { @class = "col-md-5 text-danger" })

I inspected the element and it showed as below:

enter image description here

and

enter image description here

On breakpoint we can see the value for ToolingID

enter image description here

But not the ToolingID_Ori

enter image description here

I've been searching for solution but they mention the name must be the same and I need to put the prefix bind. But how do I do this for hidden input?

Khairul
  • 191
  • 2
  • 14
  • You've not bound it to anything! You set ToolingID_Ori from ViewBag and render it in a hidden control... not hiddenFor! Why dont you just add ToolingID_Ori to your model? – Wheels73 Jun 19 '17 at 09:19
  • Well, even `BindAttribute` itself doesn't include `ToolingID_Ori` at the model binding schema. Use a viewmodel class with string properties and bind it with strongly-typed helpers e.g. `HiddenFor` instead. – Tetsuya Yamamoto Jun 19 '17 at 09:21
  • You have not shown us the correct code. You have set the value of `ViewBag.ToolingID_Ori` to "lalala" in the controller but the image of the html shows the value of the hidden inputas `Tool A" –  Jun 19 '17 at 10:12
  • And as a side note, its just `[Bind(Prefix = "M_Tooling")]` –  Jun 19 '17 at 10:13
  • Oh my! Did not see that.. thank you very much guys! – Khairul Jun 19 '17 at 20:03
  • Why in the world did you accept that answer - it has nothing whatsoever to do with your issue. Using `@Html.Hidden()` or `@Html.HiddenFor()` makes no difference –  Jun 20 '17 at 10:08

1 Answers1

1

You need to use an HiddenFor to map your Model to the view.

  • OP is using `@Html.Hidden()` which is fine. `HiddenFor()` might be better practice, but it make no difference (and it appears OP does not have a property in the model for `ToolingID_Ori` anyway so they could not use `HiddenFor()` –  Jun 19 '17 at 10:11
  • Its Ok I did not see that.. changing to use `@HiddenFor()` and include `ToolingID_Ori` in the model would be fine with me. Thank you again guys for your kind attention. – Khairul Jun 19 '17 at 20:05