Load Model Variable from JavaScripts

0

I have a Razor C # View in which I have a button. When I click on the button I capture by means of JS its respective ID, what I want is for that ID obtained, to be loaded in a current Model Attribute, for example @ Modelo.Id

my button:

< input type="button" name="pagin" value=@(i+1) id=@i onclick="return Pag(this)" />

Js method for OnClick:

    function Pag(button) {
        alert(button.value);

        return false;
    }

what I have inside alert, I want to load it to the model.

    
asked by Pablo.P 12.07.2018 в 19:59
source

1 answer

0

You can take this as a base:

CSHTML (Main view):

    @HtmlPartial ("VistaParcila", Model)

CSHTML (Partial View)

<input type="button" name="pagin" id="@i" onclick="Pag(@(i+1))" />

JS:

function Pag(value) {
        alert(value);

$. ajax ({   method: "POST",   url: "tucontrolador / SetProperty",   data: {id: @id, newId: value} success: function (data) { $ ("container"). html (data); } }) }

Controller:

public class TuControlador:Controller{

public ActionResult EstablecerPropiedad(int id, int nuevoId){
  var modelo = ObtenerModelo(id);  //Aca debes obtener tu modelo de tu origen de datos por medio de su id o algo similar

  modelo.Id = nuevoId;
  //Aca solo se ha asignado la propiedad, no se ha guardado en el origen de datos

  return PartiaView(modelo); //Esta vista ya tiene el modelo con el nuevo id
}
}

Greetings.

    
answered by 12.07.2018 в 20:18