change value of received model properties in controller MVC C #

3

I have an ASP.NET MVC application in which, through a controller, I receive a model from a view (the MVC standard) as shown.

 [HttpPost]
            public ActionResult EnviarEALG(Modelo datos)
            { }

But now by necessity I must present that model that I receive in the controller in a view but with a change in one of its string properties called action which comes with the value of "new" but when I resend the model I want to send it with the value of "second record". I also want that three specific properties are not returned with the value received but the value are empty strings, for now I do so

  [HttpPost]
        public ActionResult EnviarEALG(Modelo datos)
        {
                datos.accion = "nuevo valor";
                datos.nombres = "";
                datos.cargo = "";
                datos.nivel = "";
            return View(datos);
        }

What I have decided is to use ViewBag to send the value to the view and then using javascript to assign the new value to the desired property in this way (idAction is the id of the Html.EditorFor)

<script type="text/javascript">
    var accion = '@ViewBag.accion';
    alert(accion);
    $("#idAccion").val('');
    $("#idAccion").val(accion);   
</script>

In the JavaScript Alert I get the desired value but even with $("#idAccion").val(accion); in the strongly typed view (Model) I always see the original values, how can I change the values in the controller and receive the values I want? in my sight? thanks.

These are the properties that I want to change that are in the Model class.

 public string accion { get; set; }
 public string nombres { get; set; }
 public string cargo { get; set; }
 public string nivel { get; set; }

and in the view, this is one of the property to which I want to change the value

<div class="form-group">
    @Html.LabelFor(model => model.accion, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
      @Html.EditorFor(model => model.accion, new { htmlAttributes = new { @class = "form-control", @id = "idAccion", @value = "@ViewBag.accion" } })
        @Html.ValidationMessageFor(model => model.accion, "", new { @class = "text-danger" })
    </div>
</div>

but I still see the original value, it does not change it.

    
asked by Pablo Mayora 27.03.2017 в 22:42
source

1 answer

3

Instead of changing the value of the properties of the model from the controller, I send a value through ViewBag and then I assign it to the property of the model that interests me, so in the controller I think

 ViewBag.accion = "solicitud creada con un empleado registrado";

and in the view I assign it in this way

  @Html.EditorFor(model => model.accion, new { htmlAttributes = new { @class = "form-control", @Value = ViewBag.accion, @id = "idAccion" } })
    
answered by 28.03.2017 / 14:51
source