'How to get a CheckBox value in MVC C#
I'm starting with MVC C#.
Sorry if this is an a very easy question.
I'm trying to get a CheckBox value from the view. The idea is put some checkboxes on the view, convert these values to Int32, to concatenate on a string entity for afterlly save in my DB. The checkboxes should not be linked to the model.
Could someone explain me how to do it and how to link the checkbox to the controller? I'm very confused.
This is my model.
namespace Crossdock.Models{
public class Roles
{
[Key]
[Column(Order = 0)]
public int RolID { get; set; }
[Display(Name = "Tipo de Usuario")]
public string Descripcion { get; set; }
[Display(Name = "Permisos")]
public string Permisos { get;set;}
//Permisos a validar por Área
//Guías
public bool GeneracionGuias { get; set; }
//CEDIS
public bool RecepcionPaquetes { get; set; }
public bool CrossdockTemporal { get; set; }
//Administración
public bool Catalogos { get; set; }
}
}
This is my controller code. Here is where i need parse the bool values from the model to int/string and concatenate for save on "Descripcion" string field.
// GET: Roles/Create
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Roles cl)
{
if (!ModelState.IsValid)
{
return RedirectToAction("Index");
}
var rl = new Roles
{
RolID = 0,
Descripcion=cl.Descripcion,
Permisos=cl.Permisos,
};
objRol.Alta_Roles(rl);
return RedirectToAction("Index", new { area = "" });
}
This is my Create View.
@model Crossdock.Models.Roles
@{
ViewBag.Title = "Create";
}
<hr />
<h1>Registrar Nuevo Perfil de Permisos</h1>
<hr />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Descripcion, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Descripcion, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Descripcion, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Permisos, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Permisos, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Permisos, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="container">
<div class="row justify-content-center">
<div class="col-4">
<h3>
Guías
</h3>
<span> | Generación de Guías: </span> @Html.CheckBoxFor(m => m.GeneracionGuias, true)
</div>
<div class="col-4">
<h3>
CEDIS
</h3>
<span> | Recepción de Paquetes: </span> @Html.CheckBoxFor(m => m.RecepcionPaquetes, false)
<br />
<span> | Crossdock Temporal: </span> @Html.CheckBoxFor(m => m.CrossdockTemporal, false)
</div>
<div class="col-4">
<h3>
Administración
</h3>
<span> | Catálogos: </span> @Html.CheckBoxFor(m => m.Catalogos, false)
</div>
</div>
</div>
<hr />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Registrar" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
<br />
@Html.ActionLink("Regresar", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
