Home Checkbox value always 0 after insert into database
Reply: 1

Checkbox value always 0 after insert into database

Raspi Surya
Raspi Surya Published in 2017-11-10 01:24:41Z

I am building a website using Asp.Net MVC. I have successfully inserted my form data into the database. However, I got a problem with my checkbox, the value is always 0 in my database even if the checkbox is checked.

@using (Html.BeginForm())
    <div class="modal-body">
        <div class="form-group">
            <label for="username" class="col-form-label">Name:</label>
            <input type="text" class="form-control" **id="username" name="username" **>
        <div class="row">
            <div class="form-group">
                <div class="col-md-6">
                    <input type="checkbox" **id="user_privilage" name="user_privilage" **>
                    <label for="user" class="col-form-label">User</label>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Add</button>

here is the code in controller:

public ActionResult UserMaintenance(User collection)
        List<object> lst = new List<object>();
        object[] allitems = lst.ToArray();
        int output = db.Database.ExecuteSqlCommand("insert into DATABASE(username,user_privilage) values (@p0,@p1)", allitems);
            ViewBag.msg = "User has been added";
        return View();
        //return RedirectToAction("UserMaintenance");
    catch {
        return View();

Does anyone know what happened?

Saineshwar Reply to 2017-11-10 04:14:19Z

You are using pure html markup checkbox in that you need to set value to checkbox as true.



Views at Runtime

POST Action Method

We have checked checkbox and submitted form we must get value true in user_privilage model property.

In Same way if we do for unchecking checkbox then we will get false value.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.4387 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO