lundi 6 avril 2015

Getting selected values from checkbox from View

I'm trying to get the selected value from the check box from the view and pass it to the controller to update the database value of true/false.


Below is my view:



@using (Html.BeginForm("EditWrapups", "Items", FormMethod.Post))
{
{
var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 10, defaultSort: "Id");
@grid.GetHtml(tableStyle: "PGrid", headerStyle: "Header", alternatingRowStyle: "altRow", htmlAttributes: new { id = "DataTable" }, columns: grid.Columns(
grid.Column("Id", "Product ID"),
grid.Column("WrapupNames", "Wrapup Names"),
grid.Column("IsAvailable", "Product Available", format: @<text><input name="IsAvailable" type="checkbox" value="@item.IsAvailable" @(item.IsAvailable == true ? "Checked" : null) /></text>)));
}


<br />
<div class="form-group">
<div class="width">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>


}


Here is my controller:



[HttpGet]
public ActionResult EditWrapups()
{
var wraups = new List<GetWrapupType>();
using ( _context = new DatabaseContext())
{
wraups = _context.GetWrapupTypes.ToList();
}
return View(wraups);
}

[HttpPost]
public ActionResult EditWrapups(GetWrapupTypes typeToAdd)
{

_context.GetWrapupTypes.Add();
_context.SaveChanges();
return View();
}


Here is my model:



public partial class GetWrapupType
{
public int Id { get; set; }
public Nullable<bool> IsAvailable { get; set; }
public string WrapupNames { get; set; }
}


}


I am using Entity Framework to update the changes to my database. I am having troubles updating the database from the selected values from the view.





Aucun commentaire:

Enregistrer un commentaire