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>

7
  • 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. Commented Mar 20, 2018 at 18:37
  • Essa parte eu consigo fazer normalmente. É que eu preciso fazer um postback quando eu selecionar um item no DropDownList. Commented Mar 20, 2018 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. Commented Mar 20, 2018 at 18:56
  • See here: stackoverflow.com/questions/33947882/… Commented Mar 20, 2018 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. Commented Mar 20, 2018 at 22:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.