CheckBox in Asp.Net MVC (Insert Data of CheckBox in Database and GetData Of checkBox From DataBase)
Insert Data of CheckBox in Database
Controller
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Final_Form model, bool Csharp, bool Fsharp, bool JAVA, bool php)
{
Final_Form final_Form = new Final_Form();
final_Form.Intrested_C_ = (Csharp==true)? true:false;
final_Form.Intrested_F_ = (Fsharp==true)? true:false;
final_Form.Intrested_JAVA = (JAVA==true)?true:false;
final_Form.Intrested_PHP = (php==true)?true:false;
_context.Final_Form.Add(final_Form);
_context.SaveChanges();
return RedirectToAction("index");
}
View
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Final_Form</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label class="control-label col-md-2">
Intrest
</label>
<div class="col-lg-10">
<label class="checkbox-inline">
@Html.CheckBox("Csharp", false)
C#
</label>
<label class="checkbox-inline">
@Html.CheckBox("Fsharp", false)
F#
</label>
<label class="checkbox-inline">
@Html.CheckBox("JAVA", false)
JAVA
</label>
<label class="checkbox-inline">
@Html.CheckBox("php", false)
PHP
</label>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<br />
<input type="submit" value="Submit" id="submit" class="btn-success" />
</div>
</div>
</div>
</div>
}
Get Data From DataBase in checkBox
Edit(Update)
Controller
[HttpGet]
public ActionResult Edit(int id)
{
var result = _context.Final_Form.Find(id);
result.Intrested_C_ = (result.Intrested_C_ == null) ? false : result.Intrested_C_;
result.Intrested_F_ = (result.Intrested_F_ == null) ? false : result.Intrested_F_;
result.Intrested_JAVA = (result.Intrested_JAVA == null) ? false : result.Intrested_JAVA;
result.Intrested_PHP = (result.Intrested_PHP == null) ? false : result.Intrested_PHP;
return View(result);
}
[HttpPost]
public ActionResult Edit(Final_Form final_Form, bool Csharp, bool Fsharp, bool JAVA, bool php)
{
final_Form.Intrested_C_ = (Csharp == true) ? true : false;
final_Form.Intrested_F_ = (Fsharp == true) ? true : false;
final_Form.Intrested_JAVA = (JAVA == true) ? true : false;
final_Form.Intrested_PHP = (php == true) ? true : false;
_context.Entry(final_Form).State = EntityState.Modified;
_context.SaveChanges();
return RedirectToAction("index");
}
View
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
<label class="control-label col-md-2">
Intrest
</label>
<div class="col-lg-10">
<label class="checkbox-inline">
@Html.CheckBox("Csharp", Model.Intrested_C_.Value)
C#
</label>
<label class="checkbox-inline">
@Html.CheckBox("Fsharp", Model.Intrested_F_.Value)
F#
</label>
<label class="checkbox-inline">
@Html.CheckBox("JAVA", Model.Intrested_JAVA.Value)
JAVA
</label>
<label class="checkbox-inline">
@Html.CheckBox("php", Model.Intrested_PHP.Value)
PHP
</label>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
Comments
Post a Comment