0

I am trying to use an MVC form to modify some information in my database. I want to be able to select a few items from a table using a series check boxes. It should update the database boolean values when I hit a link at the bottom of my form.

So far, I have tried a few solutions from other threads, but since I am new to MVCs, they are rather confusing.

This is what I have right now for my HTML:

    @foreach (var item in Model)
    {
    <tr>
        @if (!item.IsCurated)
        {
            <td>
                @Html.CheckBoxFor(modelItem => item.isChecked, new { @checked = true })
            </td>
        {
    </tr>

    @Html.ActionLink("Update", "updateDatabase", Model)

The "updateDatabase" method calls

public void updateDatabase()
{
    db.SaveChanges();
}

I believe the changes to the database are being saved, but that the check boxes are not actually assigning any changed values.

ekad
  • 14,436
  • 26
  • 44
  • 46
  • 2
    ActionLink helper method generates a link, clicking on which does a GET request for that url. It will not send your checkbox data ! You need to have a form and submit the form. In the HttpPost action method which handles the form submission, you can update your db. – Shyju Sep 06 '16 at 17:26
  • How might I go about that? As I said above, I'm new to MVCs (2 days of toying with it). – Daniel Kasman Sep 06 '16 at 17:29
  • I suggest you read the basics of how mvc works(Ex : [Getting Started with ASP.NET MVC 5](http://www.asp.net/mvc/overview/getting-started/introduction/getting-started)) and practice it. This should give you a good basic understanding – Shyju Sep 06 '16 at 17:30
  • You can use ajax to do this simple things, EX:` $(document).ready(function(){ $("#isChecked").change(function(){ $.ajax{ type : "post or get here" , url: '@Url.Action("actionName","ControllerName")'+'/'+$(this).val(), success: function(data){ //blablabla } } }) })` – Lucas Sep 06 '16 at 19:50
  • If you're not familiar with how forms work, I'd suggest starting with http://docs.webplatform.org/wiki/guides/html_forms_basics or https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms, and then returning to the MVC project when you understand the underlying technology – Tieson T. Sep 06 '16 at 23:33

0 Answers0