DropDownList in views asp.net mvc error in validation

0

In view razor of asp.net mvc I have a dropdownlist with the following code:

@Html.DropDownListFor(model => model.Banco, (SelectList) ViewBag.Banco, new {@class = "form-control"})

Even if you select the value, when it is valid in controller , as ModelState.IsValid , always the error, it says that the selected value is invalid, but, in fact, it is not, Id selected is correct. Can you ignore the validation of this field or fix it?

    
asked by anonymous 11.01.2016 / 13:10

2 answers

0

I was able to solve the problem, I was doing wrong, I changed to point to the Bank Id and not to the bank itself and it worked:

@ Html.DropDownListFor (model => model.Banco.Id, (SelectList) ViewBag.Banco, new {@class="form-control"})

    
13.01.2016 / 13:04
1

You can do in the scope of an Action that receives the template of your submitted form

[HttpPost]
public ActionResult Edit([Bind(Exclude="Banco")]CompanyDto model)
{
    // ...
}

You can discriminate the property in the scope of your model also since in this way it will be influenced throughout the application and not just the Action being used.

[Bind(Exclude="Propriedade_1,Propriedade_2,Propriedade_3")]
public class CompanyDto
{
  public int Id { get; set; }
  public string Propriedade_1 { get; set; }
  public string Propriedade_2 { get; set; }
  public string Propriedade_3 { get; set; }
  // ...
}

Can also filter a property of an example model object

public class CompanyDto
{
  public int Id { get; set; }
  [Exclude]
  public string Propriedade_1 { get; set; }
  public string Propriedade_2 { get; set; }
  public string Propriedade_3 { get; set; }
  // ...
}

Another way to check is to put a breakpoint in Action that receives the data and before that in the browser press F12 and check the value of the item that has the 'selected' property of your dropdown before you submit the form to Action .

    
11.01.2016 / 19:51