通过ajax将复选框传递给控制器

时间:2021-12-20 08:28:31

I am developing a project on ASP.net MVC.

我正在开发一个ASP.net MVC的项目。

I have a html form with some checkboxes say A,B,C,D & E and I am posting the form through ajax to one of the controllers.

我有一个带有复选框的html表单,比如a、B、C、D和E,我通过ajax将表单发布到其中一个控制器。

I would like to distinctly identify if each check boxes are checked from the controller and perform some action based on the selected checkbox value.

我想明确地标识是否从控制器中选中了每个复选框,并基于所选的复选框值执行一些操作。

I would like to know the best practise to acheive this.

我想知道做这件事最好的办法。

P.S: I will be passing the selected checkbox value to the underlying DAL.

P。我将把选中的复选框值传递给下一个DAL。

1 个解决方案

#1


2  

You can do it in many ways. Here i'm giving a sample idea-

你可以用很多方法。这里我给出一个样本的概念。

Using a model object

使用一个模型对象

Lets assume, you have a model "CheckBoxValues" in server side with the fields

假设在服务器端有一个带有字段的“CheckBoxValues”模型

public class CheckBoxValues
{
    public Boolean A { get; set; }
    public Boolean B { get; set; }
}

On your html page, use the code to get values from the checkboxes on a button click handler-

在html页面上,使用代码从按钮单击处理程序的复选框中获取值

var values= {};
var StateOfCheckBoxA = $('#CheckBoxA').is(':checked'); 
values.A= StateOfCheckBoxA  ;
var StateOfCheckBoxB = $('#CheckBoxB').is(':checked'); 
values.B= StateOfCheckBoxB;

var SubmitURL = YourController/ActionMethod/


            $.ajax({
                type: "POST",
                url: SubmitURL,
                data: values,
                dataType: 'json',

                beforeSend: function () {

                },
                success: function (result) {

                },
                error: function (request, status, error) {

                },
                complete: function () {

                }
            });

Now your actionmethod

现在你actionmethod

 public JsonResult Create(CheckBoxValues values)
 {
       Boolean checkboxA=values.A;
       Boolean checkboxB=values.B;
 }

Hope this will help you.

希望这能对你有所帮助。

#1


2  

You can do it in many ways. Here i'm giving a sample idea-

你可以用很多方法。这里我给出一个样本的概念。

Using a model object

使用一个模型对象

Lets assume, you have a model "CheckBoxValues" in server side with the fields

假设在服务器端有一个带有字段的“CheckBoxValues”模型

public class CheckBoxValues
{
    public Boolean A { get; set; }
    public Boolean B { get; set; }
}

On your html page, use the code to get values from the checkboxes on a button click handler-

在html页面上,使用代码从按钮单击处理程序的复选框中获取值

var values= {};
var StateOfCheckBoxA = $('#CheckBoxA').is(':checked'); 
values.A= StateOfCheckBoxA  ;
var StateOfCheckBoxB = $('#CheckBoxB').is(':checked'); 
values.B= StateOfCheckBoxB;

var SubmitURL = YourController/ActionMethod/


            $.ajax({
                type: "POST",
                url: SubmitURL,
                data: values,
                dataType: 'json',

                beforeSend: function () {

                },
                success: function (result) {

                },
                error: function (request, status, error) {

                },
                complete: function () {

                }
            });

Now your actionmethod

现在你actionmethod

 public JsonResult Create(CheckBoxValues values)
 {
       Boolean checkboxA=values.A;
       Boolean checkboxB=values.B;
 }

Hope this will help you.

希望这能对你有所帮助。