Need to update the check box value to db table when i click the checkbox() without post back in MVC2

后端 未结 3 1123
萌比男神i
萌比男神i 2021-01-27 06:40



相关标签:
3条回答
  • 2021-01-27 06:58

    You have to do some sort of request back to the server, whether it's a POST from a form button or an Ajax POST or GET request.

    Form button:

    <form action="/MyApp/HandleClick/" method="post">
        <input type="checkbox" name="SelectedObject" value="cbValue"/>
        <button type="submit">Submit</button>
    </form>
    

    Or, Ajax (with jquery):

     jQuery('input[name=SelectedObject]').click(function() {
         jQuery.ajax({
             url: '/MyApp/HandleClick/',
             data: {
                 SelectedObject: this.value,
             }
             success: function() {
                 // Process success data...
             }
         });
     });
    

    Then your controller:

    public class MyAppController : Controller
    {
        [HttpPost]
        public ActionResult HandleClick(string value)
        {
            // Handle persisting value to database...
    
            // If posting
            return RedirectToAction("OtherAction");
    
            // If Ajax
            return Json("Success!");
        }
    }
    

    That's the simplest example - can't answer more without more details about exactly what you're trying to accomplish.

    0 讨论(0)
  • 2021-01-27 07:05
    $('#checkboxid').click(function(){
     $.ajax({ url: 'your_url_for_receiving_data',
    type: 'POST',
    data: { checkbox: $('#checkboxid').attr('checked') },
    success: function(o) { alert('saved'); }
    });
    

    just create code behind in mvc2 where you will get value from request and save it to db

    0 讨论(0)
  • 2021-01-27 07:07

    " />

    in controller

    public ActionResult(string value) { return View();

    } it is help you

    0 讨论(0)
提交回复
热议问题