Popular a View with values in the ASP.NET MVC model

Asked

Viewed 123 times

0

In the Index Method I can set values in the model and initialize my VIEW with predefined value. But when I do the POST for the START method, when I change the Aquedica property, the value is not being passed to the view, it actually happens with any property that I change the value of the model. NOTE: I tried to use the form below where I found 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>
}
  • You’re starting the View again, where you preload the data, the idea is you return only the model!

  • @Marconi, I don’t understand your comment! Could detail better?

  • Puts a break point on Index()!

  • Putzzzz, I got it.. As I’m calling View("Index", model), it will go through my Action Index again, that’s right?

  • That’s right Nicola rs, I just know curious Asp.net MVC, if I’m not mistaken you have to return a Jsonresult

  • Or I can create another View for my Start Action, and another one for Homerapido! Comprreendi! Thanks.. rs

  • @Nicolabogar, did you solve it? took a look at my answer?

  • @Leandroangelo, I tried to do the way you said but I could not, I solved my problem returned a Json result and populating my components with Jquery

Show 3 more comments

1 answer

0


You are changing the attribute of a Viewmodel you received as input 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.