Problem running method to insert new User using ASP.NET MVC with HTML and RAZOR

Asked

Viewed 187 times

1

I have a problem running a request to enter a new user, I do not know what I need to do now to finalize my request, By clicking the sign up button nothing happens below are my HTML code and the controller (Controller Name: Usuariocontroller)

<form data-toggle="validator" role="form">
    @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label" }) <br>
        @Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control", id = "Nome", placeholder = "Nome", required = "required" } })
        @Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
    </div>
    <!--                    Confirmação de password                      -->
    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label" }) <br>
        @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", id = "Email", type = "Email", placeholder = "Email", required = "required" } })
        @Html.ValidationMessageFor(model => model.Email, "", new { @class = "help-block with-errors" })
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Senha, htmlAttributes: new { @class = "control-label" }) <br>
        <div class="form-inline row">
            <div class="form-group col-sm-6">
                @Html.EditorFor(model => model.Senha, new { htmlAttributes = new { @class = "form-control", id = "inputPassword", placeholder = "Password", required = "required", minlenght = "6", type = "password" } })
                @Html.ValidationMessageFor(model => model.Senha, "", new { @class = "help-block with-errors" })
            </div>
            <div class="form-group col-sm-6">
                <input type="password" class="form-control" id="inputPasswordConfirm" data-match="#inputPassword" data-match-error="Opa, as senhas não batem" placeholder="Confirm" required>
                <div class="help-block with-errors"></div>
            </div>
        </div>
    </div>

    <div class="form-group">
        @using (Html.BeginForm("Adicionar", "Usuario", FormMethod.Get))
        {
            <button type="submit" value="Create" class="btn btn-success">Cadastrar</button>
        }
        <button type="button" class="btn btn-success" id="btn-login">Login</button>
    </div>
</form>

<> // GET: User [Httpget] public Actionresult Index() { Return View("Index", Repo.List()); }

[HttpPost]
public ActionResult Adicionar(Usuario user)
{
    Repo.Adicionar(user);
    return View("Index");
}

[HttpPost]
public ActionResult Atualizar(Usuario user)
{
    Repo.Atualizar(user);
    return View("~\\Views\\Principal\\Principal.cshtml");//?
}

// DELETE : Usuario
[HttpDelete]
public ActionResult Delete(int id)
{
    Repo.Deletar(id);
    return View("Index", Repo.Listar());
}
  • 1

    Your question is not very clear, try editing. When editing I suggest you describe more details of the problem/error that is occurring. Analyzing the code I notice some strange things, but not to infer an answer without knowing exactly the problem.

  • You didn’t report the problem you’re having... report the problem.. And by looking at yours Controller you defined two methods with the same name AddUsuario, without specifying which type requests will be received POSTtry to put the DataAnotation``HttpPost in the method that receives the ViewModelas a parameter...

1 answer

0


The helper Html.Beginform that marks the beginning of a form does not contemplate its fields.

Analyzing your code is as if in your form there was only one button:

<div class="form-group">
        @using (Html.BeginForm("Adicionar", "Usuario", FormMethod.Get))
        {
            <button type="submit" value="Create" class="btn btn-success">Cadastrar</button>
        }
        <button type="button" class="btn btn-success" id="btn-login">Login</button>
</div>

Adjust your code by including in the form (within the helper Html.Beginform) all fields and how in your controller you define [HttpPost] in the Add action, also change the Formmethod.Get parameter to FormMethod.Post:

...
<div class="form-group">
        @using (Html.BeginForm("Adicionar", "Usuario", FormMethod.Post))
        {

        <!-- INSIRA O CÓDIGO DO FORMULÁRIO AQUI -->

            <button type="submit" value="Create" class="btn btn-success">Cadastrar</button>
        }
        <button type="button" class="btn btn-success" id="btn-login">Login</button>
</div>
...

Example:

Model:

public class Usuario
{
    public String Nome { get; set; }

    public String Email { get; set; }

    public String Senha { get; set; }
}

View:

@model WebApplicationMVC.Models.Usuario

@{
    ViewBag.Title = "Adicionar";
}

<h2>Adicionar</h2>

@*A linha de baixo ou @using (Html.BeginForm("Adicionar", "Usuario", FormMethod.Post))*@
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Usuario</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Senha, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Senha, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Senha, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

Controller:

    [HttpGet]
    public ActionResult Adicionar()
    {            
        return View();
    }

    [HttpPost]
    public ActionResult Adicionar(Usuario user)
    {
        Repo.Adicionar(user);
        return View("Index");
    }

Form:

inserir a descrição da imagem aqui

Debugging sent values:

inserir a descrição da imagem aqui

  • It’s not sending the request yet but maybe I know the reason... I need Html.Submit button instead of html is not?

  • I edited the answer and created a complete example for you. Based on it make tests and adjust.

Browser other questions tagged

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