WebGrid with RadioButton

1

I want to select a WebGrid line through a RadioButton and click the Preview button, / em> with the selected line information.

How do I pass information from the selected line to controller ?

CSHTML:

@model IEnumerable<WebAppEight.Models.ContratosUnidade>
<link href="~/Content/Perfil/webgrid-style.css" rel="stylesheet" />
<link href="~/Content/Perfil/perfil-style.css" rel="stylesheet" />

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
WebGrid grid = new WebGrid(Model, rowsPerPage: 6);
}

@using (Html.BeginForm("ContratoSelecionado", "Contratos", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @grid.GetHtml(
        tableStyle: "webgrid-table",
        headerStyle: "webgrid-header",
        footerStyle: "webgrid-footer",
        alternatingRowStyle: "webgrid-alternating-row",
        rowStyle: "webgrid-row-style",

        fillEmptyRows: true,
        mode: WebGridPagerModes.All,
        columns: new[]
        {
            grid.Column(format:@<text><input type="radio" 
                                 id="contratosUnidade" 
                                 name="contratosUnidade" 
                                 checked="@item.Codigo" /></text>, 
                                 style: "webgrid-select-column"),
            grid.Column("Codigo", "Código"),
            grid.Column("FormaContratacao", "Forma de Contratação")
       }
   )

<div class="ddldiv">
    <input class="input-buttons-footer" type="submit" value="Visualizar" />
    <input class="input-buttons-footer" type="submit" value="Editar" />
    <input class="input-buttons-footer" type="submit" value="Novo" />
    <input class="input-buttons-footer" type="submit" value="<" />
</div>

}

Controller (which should receive some information from the selected line):

  public ActionResult ContratoSelecionado(string codigo)
  {
      return View();
  }

Obs: The controller ContratoSelecionado is called, however, without parameters.

    
asked by anonymous 21.01.2017 / 21:50

2 answers

0

To view by clicking check-box, you can also use the following snippet:

    grid.Column(header: "Visualizar?", format: @<text><input class="check-box" id="assignChkBx" name="assignChkBx" type="checkbox" value="@item.Id" /></text>),

And no javascript:

    $(document).ready(function () {
    $("#assignChkBx").click(function () {
        var id = $('#assignChkBx').val();
        $.ajax({
        url: '/Home/ContratoSelecionado',
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: id,
        success: function (data) {
        //alert('sucesso!!');
      },
        error: function (error) {
        //loading(0, "");
      }
    });
   });
});
    
04.03.2017 / 02:24
-1
    @{var grid = new WebGrid(
                    canPage: true,
                    rowsPerPage: Model.PageSize,
                    canSort: true,
                    ajaxUpdateContainerId: "grid");

    grid.Bind(Model.Content, rowCount: Model.TotalRecords, autoSortAndPage: false);
    grid.Pager(WebGridPagerModes.All);

    @grid.GetHtml(htmlAttributes: new { id = "grid" },   // id for ajaxUpdateContainerId parameter
        fillEmptyRows: false,
        tableStyle: "table table-bordered table-hover",
        mode: WebGridPagerModes.All,
        columns: grid.Columns(
          grid.Column("Id", "Nº Pedido"),
                    grid.Column("Descricao", "Descricao", style: "col-lg-3"),
                    grid.Column("Marca", "Marca", style: "col-lg-1"),
                    grid.Column("Fornecedor", "Fornecedor", style: "col-lg-2"),
                    grid.Column("Valor Unitário", "Valor Unitario", style: "col-lg-1", format: @<text>@String.Format("{0:C}", item.ValorUnitario) </text>),
                    grid.Column("Quantidade", "Qtd"),
                    grid.Column("Valor Total", "Valor", style: "col-lg-1", format: @<text>@String.Format("{0:C}", item.Valor) </text>),
                    grid.Column("Prazo", "Prazo", style: "col-lg-1"),
                    grid.Column("Status", "Status", style: "col-lg-1"),

                   //grid.Column(header: "Aprovar?", format: @<text><input class="check-box" id="assignChkBx" name="assignChkBx" type="checkbox" value="@item.Id" /></text>),
        //grid.Column("Email", header: "Email", format: @<text>@String.Format("{0:C}", item.Price) </text>),
          grid.Column(header: "Ações", canSort: false, style: "action",
            format: @<text>
            @Html.ActionLink(" ", "Edit", new { id = item.Id }, new { @class = "glyphicon glyphicon-edit", title = "Editar" })
            @Html.ActionLink(" ", "Details", new { id = item.Id }, new { @class = "glyphicon glyphicon-search", title = "Visualizar" })
            @Html.ActionLink(" ", "Delete", new { id = item.Id }, new { @class = "glyphicon glyphicon-trash", title = "Deletar" }) 
             </text>)

                                                   ));
        }

And try to use the controller like this ... And on the Controller:

    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Orcamento orcamento = db.Orcamentos.Find(id);
        if (orcamento == null)
        {
            return HttpNotFound();
        }
        return View(orcamento);
    }

That is, you need to pass the id on the call of your controller.

@Html.ActionLink(" ", "Details", new { id = item.Id }, new { @class =    "glyphicon glyphicon-search", title = "Visualizar" })
    
02.03.2017 / 17:53