Send data to the controller

0

I defined a default value in a form that I have in a view (@ Value="afar1793") and I set it disabled so that it could not be modified but when it gets to the controller it becomes null.

I have this in my code:

View:

<div class="form-group">
  @Html.LabelFor(model => model.Id_Usuario, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
      @Html.EditorFor(model => model.Id_Usuario, Usuario, new { htmlAttributes = new { @Value ="afar1793", @disabled = "disabled", @class = "form-control" } })
      @Html.ValidationMessageFor(model => model.Id_Usuario, "", new {@class = "text-danger" })
    </div>
 </div>  

Controller:

public ActionResult Registrar(Usuarios usu)
{
//Codigo
}
    
asked by afar1793 27.03.2017 в 01:33
source

1 answer

0

When you use a form element (a submiteable element) and you set the disabled attribute, this means that in most modern browsers it is not sent when the form is submitted. You can see more info at W3C > Html4.1 > Forms > disabled

That is, it is a "normal" behavior of HTTP forms with disabled elements

Now, if the idea is to visualize an element so as not to modify it to the user but send it: You must prevent it from being modified at any time. That is, render it in the form, but with a text simply (Imagine if the user could modify it in the submit, which can be done over or disabled, or send it in a request) If it is a value or text that should be visible, you can place it but in the already set controller, and in the response (regenerate it again). Of course, it depends on how this value should be generated

There is also the possibility of using post by javascript (ajax), for example an angular controller that makes the response by some service / factory, or some other method ... but always http requests by javascript ... and here you can get the values of "where you want" (even disabled items) and there is another story

But if the idea is simple of submit buttons that do post of the form, the disabled elements will not be sent.

I hope it will help or guide you ...

    
answered by 27.03.2017 в 05:17