Popular a View with Values in ASP.NET MVC Model

0

In the Index Method I can set values in the model and initialize my VIEW with pre-defined values. But when I do the POST to the START method, when I change the Aquedica property, the value is not being passed to the view, this actually happens with any property that I change the value of the model. NOTE: I tried to use it in the form below where I found it in STACK ENGLISH but nothing solved.

ModelState.SetModelValue("PropertyID", new ValueProviderResult("New value", "", CultureInfo.InvariantCulture));



public class NivelUmController : Controller
{

    private BONivelUm _BONivelUm;

    // GET: NivelUm
    public ActionResult Index()
    {
        NivelUmModel model = new NivelUmModel();
        model.Potencia = 10;
        model.Tempo = 0;
        return View("Index", model);
    }

    [HttpPost]
    public ActionResult Iniciar(NivelUmModel model)
    {
        if (ModelState.IsValid)
        {
            _BONivelUm = new BONivelUm();
            _BONivelUm.Microondas.Potencia = 7;
            _BONivelUm.Microondas.Tempo = 20;

            model.Aquecida = _BONivelUm.Iniciar();
        }
        return View("Index", model);
    }

    [HttpPost]
    public ActionResult IniciarRapido(NivelUmModel model)
    {
        if (ModelState.IsValid)
        {
            _BONivelUm = new BONivelUm();
            model.Aquecida = _BONivelUm.IniciarRapido();
        }
        return View("Index", model);
    }

}

VIEW:

@model App.MicroondasDigital.Web.Models.NivelUm.NivelUmModel

@{
    ViewBag.Title = "Microondas Nível 1";
}

<div class="container">
    <div class="row">
        <h2>Parametrização</h2>

        @using (Html.BeginForm("Index", "NivelUm", FormMethod.Post))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

            <div class="form-group">
                @Html.LabelFor(m => m.Tempo, new { @class = "form-label" })
                @Html.TextBoxFor(m => m.Tempo, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.Tempo)
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.Potencia, new { @class = "form-label" })
                @Html.TextBoxFor(m => m.Potencia, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.Potencia)
            </div>
            <input class="btn btn-block" id="btnIniciar" type="button" value="Iniciar" />
            <input class="btn btn-block" id="btnIniciarRapido" type="button" value="Iniciar Rápido" />

            <br />
            <div class="form-group">
                @Html.TextAreaFor(x => x.Aquecida, 10, 10, new { @class = "form-control" })
            </div>
        }

    </div>
</div>

@section Scripts {
    <script type="text/javascript">


        $(document).ready(function () {

            $('#btnIniciar').click(function () {
                $.post("NivelUm/Iniciar", $("form").serialize(), function (data) {
                });
            });

            $('#btnIniciarRapido').click(function () {
                $.post("NivelUm/IniciarRapido", $("form").serialize(), function (data) {
                });
            });
        })

    </script>
}
    
asked by anonymous 14.06.2018 / 15:21

1 answer

0

You are changing the attribute of a ViewModel that you received as an entry in your Action, you need to remove it from ModelState to persist the change.

[HttpPost]
public ActionResult Iniciar(NivelUmModel model)
{      

    if (ModelState.IsValid)
    {
        _BONivelUm = new BONivelUm();
        _BONivelUm.Microondas.Potencia = 7;
        _BONivelUm.Microondas.Tempo = 20;

        ModelState.Remove("Aquecida");
        model.Aquecida = _BONivelUm.Iniciar();
    }
    return View("Index", model);
}
    
14.06.2018 / 15:31