jeudi 2 novembre 2017

how do I make CRUD with checkbox in ASP.NET MVC 4

I want to make Show, Create, and Edit using Checkbox

this for my Model, I use ADO.NET

public partial class cb
{
    public int id { get; set; }
    public string hobi1 { get; set; }
    public string hobi2 { get; set; }
    public string hobi3 { get; set; }
    public string nama { get; set; }
}

I dont know my Code is right or not...

This for my Controller,

public class HomeController : Controller
{
    RahmanEntities db = new RahmanEntities();
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(cb model)
    {
        cb item = new cb();
        item.nama = model.nama;
        item.hobi1 = model.hobi1;
        item.hobi2 = model.hobi2;
        item.hobi3 = model.hobi3;

        db.cbs.Add(item);
        db.SaveChanges();
        return RedirectToAction("Index", "Home");
    }

    public ActionResult ListData(cb model)
    {
        var costumerlist = db.cbs.ToList();
        ViewBag.userdetails = costumerlist;


        return View();
    }

}

I just made for Show and Create..

this for my Index View

@model checkbox.Models.cb
@using (Html.BeginForm())
{
    <div class="form-group">
        <label>Nama:</label>
        <input type="text" name="nama" />
    </div>    
    <div class="form-group">
        <label>Hobi:</label>
        <input class="form-control" type="checkbox" name="hobi1" value="Makan" />Makan &nbsp;&nbsp;
        <input class="form-control" type="checkbox" name="hobi2" value="Minum" />Minum &nbsp;&nbsp;
        <input class="form-control" type="checkbox" name="hobi3" value="Tidur" />Tidur &nbsp;&nbsp;
    </div>

    <div class="button">
        <button>Submit</button>
    </div>
}

and this for my ListEdit View

<table>
<thead>
    <tr>
        <th>Nama</th>
        <th>Hobi 1</th>
        <th>Hobi 2</th>
        <th>Hobi 3</th>
        <th>Action</th>
    </tr>
</thead>
<tbody>
    @foreach (var item in ViewBag.userdetails)
    {
        <tr>
            <td>@item.nama</td>
            <td>@item.hobi1</td>
            <td>@item.hobi2</td>
            <td>@item.hobi3</td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.id })
            </td>
        </tr>
    }
</tbody>
</table>

I dont know whats should I do..

I just want to make like CRUD using checkbox




Aucun commentaire:

Enregistrer un commentaire