2

I have a simple mvc project consisting of one table named Persons and that table has some attributes like name, age, surname and so on.

I Recently added an attribute to table which is type of bit in SQL and bool in C# and for me it represents some status. If it is ok I put that status to true or false.

In one controller I create an index view of my Persons in the database and I display the status as a checkbox. That is working ok. What I want to do is to change the status if I click on that checkbox instead of using an edit view for all of the atributes. I just want to save checkbox value from my index view to database.

Some people advised me to create an action in my controller like so:

public ActionResult Save(int id, bool status){}

Where id is id of my person in db and status is a value of the checkbox I clicked.

So how do I call that function with jquery, ajax or javascript and send those params? I am not so good with ajax and a little bit afraid of it :)

And one more thing I manage to show checkbox value corectly from my databse only in this way:

@Html.CheckBox("somename", item.Status)
        <input name="somename" type="hidden" value="false"/>

Any help or advice or sending me to good direction is good!

1 Answer 1

2

You have several options. One way would be to place the checkboxes in a (normal) form and then use the jQuery Form plugin to Ajaxify the form:

@using(Html.BeginForm()) {
    @Html.Checkbox(...)
}

<script type="text/javascript">
    $(document).ready(function () {
        $('form').ajaxForm({
            success: Saved,
            error: HandleError
        });
    });

    function Error(response, status, err) {
        // code to handle error here
    }

    function Saved(responseText, statusText, xhr, $form) {
        // code to handle succes, if any
    }
</script>

You could, of course, use an @Ajax.BeginForm as well, but this has the advantage of being easily downgradable. Just remove the jQuery form initialisation and you got yourself a regular form : )

Another possibility is to simply use an @Ajax.ActionLink. I wrote a blog post on how to deal with Ajax.ActionLink that I think will help you out.

The signature for your controller action on your question looks correct:

public ActionResult Save(int id, bool status){}

Another options is actually to create a Settings view model (call it what you will), and in there just have a bunch of bool properties to represent the checkboxes:

public class Settings
{
    public bool Status { get; set; }
    // more properties here
}

then you can bind your action to the Settings object:

public ActionResult Save(int id, Settings settings){}

This will come in handy in case you end up having multiple checkboxes to bind.

Sign up to request clarification or add additional context in comments.

5 Comments

using ajax actionlink seems to me like good choice so i could set ajax.action link on my checkbox and then call action and send params? I am reading your blog post and trying to figure is it possible to use it with my function, in actionlink i can send id and checkbox value?
@GoranB - The problem with ActionLink in this case is that you'll need a "Save" link for every checkbox. This might not be too bad if you only have the status checkbox but can get quite cluttered if you have more. And passing the value of the checkbox dynamically could get tricky too, I'm thinking...
i have status and one more checkbox also from db, so if you could write an example of actionlink on checkbox and how to send params?
@GoranB - In that case I'd definitely recommend using the form method, either with the jQuery Form plugin or with @Ajax.BeginForm.
Thank you, although i didn't use all advices you give me, you point me to right idea and i use it, i managed to complete my task without form, only using html.actionlink, it works great i just need to add for my next bool var.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.