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

风流意气都作罢 提交于 2019-12-20 07:18:42

问题


<input type="checkbox" name="n" value=1 />
<input type="checkbox" name="n" value=2 />
<input type="checkbox" name="n" value=3 />

I have above checkbox when i select the this i need to update the DB table without post back. Please explain.. If possible you can say jquery or ajax method to solve my problem


回答1:


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.




回答2:


$('#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




回答3:


" />

in controller

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

} it is help you



来源:https://stackoverflow.com/questions/6541063/need-to-update-the-check-box-value-to-db-table-when-i-click-the-checkbox-witho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!