0

I need to do something that is easy to talk, but a bit difficult to implement: Whenever in my view, a Person Nature is selected, one should trigger an event that triggers a postback, send the viewmodel to Create action so that it is edited, then it is returned to my view and my @Model is updated with the new viewmodel changed. I need to do this via JavaScript. I've already done a few posts on the subject, but nobody wants to help me, so I decided to start from scratch. Does anyone know how to help me?

enter image description here

[HttpPost]
[Authorize(Policy = "CanWriteCustomerData")]
[Route("pessoa-gerenciamento/cadastrar-novo")]
[ValidateAntiForgeryToken]
public IActionResult Create(PessoaViewModel pessoaViewModel)
{
     var pessoa = pessoaViewModel.Id = 1000;
     return View(pessoa);
}

   public class PessoaViewModel
    {
        [DisplayName("Código")]
        public int Id { get; set; }

        [DisplayName("Natureza")]
        [Required(ErrorMessage ="Escolha uma Natureza")]
        public PessoaNatureza PessoaNatureza { get; set; }
     }

@using SistemaComercial.Domain.ValueObjects
@model SistemaComercial.Application.ViewModels.Pessoa.PessoaViewModel
@{
    ViewData["Title"] = "Cadastrar Nova Pessoa";
}


<!-- begin snippet: js hide: false console: true babel: false -->
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

    <script>
    .....................Bloco JS
    </script>
}

<form asp-controller="Pessoa" asp-action="Create">
         <div class="form-group">
          <label asp-for="PessoaNatureza" class="col-md-2 control-label"></label>
            <div class="col-md-3">
              <select id="PessoaNatureza" asp-for="PessoaNatureza" asp-
                items="Model.PessoasNaturezas" class="form-control">
               <option value="" data-id="@Model.PessoaNatureza.ObterIdEnum()">--SELECIONE--</option>
              </select>
             <span asp-validation-for="PessoaNatureza" class="text-danger"></span>
         </div>
        </div>
    </form>
Master JR
  • 231
  • 2
  • 11
  • 1
    You either need to use a form in your HTML and submit the form from JS, or use AJAX to send the model data to the controller. – devlin carnate Mar 20 '18 at 18:37
  • Essa parte eu consigo fazer normalmente. É que eu preciso fazer um postback quando eu selecionar um item no DropDownList. – Master JR Mar 20 '18 at 18:42
  • If you want to post back to the controller when a drop down option is selected, you likely want to use AJAX. You can get your model data from the page, send it via AJAX to the controller, and then take some action once the AJAX call finishes. – devlin carnate Mar 20 '18 at 18:56
  • See here: https://stackoverflow.com/questions/33947882/pass-model-to-controller-using-jquery-ajax – devlin carnate Mar 20 '18 at 19:00
  • Thank you for your help. I do not know if you have had with the other line of reasoning ... In the case in question, I need to have JS do a POST in the ViewModel for a Create action. There, it will be edited (var person = personViewModel.Id = 1000;). I was able to use Ajax to give the post my #frmCreate form. An action can now be applied to a view template and edit it. – Master JR Mar 20 '18 at 22:49
  • Now, let's ask the question question: Precision has now returned a Create view template, so that it takes the place of the current view, as I've edited the Id, for example. This back is what I can not do. That's not possible? Is it possible to return a viewmodel via Ajax? How would I do that? – Master JR Mar 20 '18 at 22:50
  • $(function () { $('#PessoaNatureza').change(function () { var url = "@Url.Action("Create","Pessoa")"; var data = $('#frmCreate').serialize(); $.ajax({ type: "POST", url: '@Url.Action("Create", "Pessoa")', data: data }).done(function (res) { //$("#frmCreate").data(res); }); }); }); – Master JR Mar 20 '18 at 22:50

0 Answers0