How To insert CheckBox Data in Database -- MVC
View Code :
<div class="form-horizontal">
<h4>CheckBoxModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.song, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="form-group">
@Html.LabelFor(model => model.hoky, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.hoky, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.hoky, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.gaming, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.gaming, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.gaming, "", new { @class = "text-danger" })
</div>
</div>
Model Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Test.Views.Booking
{
public class CheckBoxModel
{
public int id { get; set; }
public string Hobbies { get; set; }
}
public class Hobbie
{
public bool song { get; set; }
public bool hoky { get; set; }
public bool gaming { get; set; }
}
}
Controller Code :
[RoutePrefix("Try")]
public class CheckBoxController : Controller
{
// GET: CheckBox
public ActionResult Index()
{
return View();
}
[Route("CheckTry")]
public ActionResult checkDemo()
{
return View();
}
[HttpPost]
[Route("CheckTry")]
public ActionResult CheckDemo(Hobbie hobbie)
{
FormDbEntities db = new FormDbEntities();
Hobbi hb = new Hobbi();
string HobiesStor="";
if(hobbie.gaming==true)
{
HobiesStor = "gaming";
}
if(hobbie.hoky==true)
{
HobiesStor = HobiesStor+ " , "+ "hoky";
}
if(hobbie.song==true)
{
HobiesStor = HobiesStor+" , "+"song";
}
hb.Hobbies= HobiesStor;
db.Hobbis.Add(hb);
db.SaveChanges();
ModelState.Clear();
return View();
}
Comments
Post a Comment