3

I have list of models and want to perform remote validation.

Model:

[System.Web.Mvc.Remote("Method", "Controller", HttpMethod = "POST", AdditionalFields = "prop2,prop3", ErrorMessage = "Error")]
 public string prop1 { get; set; }

The name generated by MVC for each elements are like below:

<input type='text' name='test[0].prop1' />

Because of this, values are not binding to the parameters. I took help from this Post. Now I am getting the value for 'prop1' but still 'prop2' and 'prop3' are not getting bound.

Edit: I am using BeginCollectionItem to render the list elements.

Any help or suggestion would be great.

Thanks in advance.

1
  • What do you have as parameter for the remote POST method? Commented Jan 11, 2019 at 18:00

3 Answers 3

2

If the remote method takes an object (as parameter) that holds its sub-items, they should be mapped automatically, for instance:

public class Stuff
{
  public List<Item> Items { get; set; }
}

public class Item
{
  [Remote(action:"Validate", controller: "Account", 
      HttpMethod = "POST", 
      ErrorMessage = "Error",
      AdditionalFields = "Prop2,Prop3")]
  public string Prop1 { get; set; }

  public string Prop2 { get; set; }
  public string Prop3 { get; set; }
}
@using (Html.BeginForm("Index", "Account"))
{
  for (int i = 0; i < Model.Items.Count; i++)
  {
    @Html.TextBoxFor(m => Model.Items[i].Prop1)
    @Html.TextBoxFor(m => Model.Items[i].Prop2)
    @Html.TextBoxFor(m => Model.Items[i].Prop3)
  }
}

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Does it help? Please let me know.
No it doesn't. I am using nuget.org/packages/BeginCollectionItem to render the list elements. That's why your answer is not helping. Please let me know if there is any other way
And you didn't mention what custom model binder you were using, until just now. That's why it's hard to tell. Thanks for letting us know anyway.
Yeah, sorry for inconvenience caused
0

By default all model properties are null except main property,

If you have another propeties to validate Bind Include them.

public ActionResult CheckThings([Bind(Include = "prop2,prop3")] Model model)
{
.....
}

If your action method dosnt accept model and you pass seperated fields to it, use

public ActionResult CheckThings([Bind(Prefix = "Prop1OrSomethigElse")]string prop1, [Bind(Prefix = "Prop2OrSomethigElse")]string prop2, [Bind(Prefix = "Prop3OrSomethigElse")]string prop3)
{
.....
}

1 Comment

How can I specify test[0] as Prefix?
0

I have taken idea from this post and fixed it by modifying the remote method in jquery-validate.js file as below:

remote: function(value, element, param) {
 ....
  param = typeof param === "string" && {url:param} || param;
  ....

  for (var property in param.data) {
    if (param.data.hasOwnProperty(property)) {
        param.data[property.substr(property.lastIndexOf(".") + 1)] = param.data[property];
      }
    }
   //ajax call

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.