Webgrid with Radiobutton

Asked

Viewed 174 times

1

I want to select a line from Webgrid through a Radiobutton and by clicking the button Visualise, carry a partial view with the information of the selected line.

How do I pass the information from the selected line to the 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.

2 answers

0

to view by clicking on the check-box, you can also use the excerpt below:

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

And in 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, "");
      }
    });
   });
});

-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 using the controller like this... And in 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 your controller call.

@Html.ActionLink(" ", "Details", new { id = item.Id }, new { @class =    "glyphicon glyphicon-search", title = "Visualizar" })
  • Adjust code formatting for better understanding.

  • I didn’t know I needed to format. I thought it was automatic.

Browser other questions tagged

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