Asp.net mvc 如何在MVC中检查复选框是否选中?
我在MVC应用程序视图中有一个复选框控件。如何检查控制器中是否已检查?这可能吗?您应该使用布尔值作为复选框的参数,以指示选中的状态和状态 获取所选复选框的ID并传递给控制器 视图中的代码:Asp.net mvc 如何在MVC中检查复选框是否选中?,asp.net-mvc,Asp.net Mvc,我在MVC应用程序视图中有一个复选框控件。如何检查控制器中是否已检查?这可能吗?您应该使用布尔值作为复选框的参数,以指示选中的状态和状态 获取所选复选框的ID并传递给控制器 视图中的代码: @model checkbox_app.Models.CheckboxModel @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="w
@model checkbox_app.Models.CheckboxModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script>
$(document).ready(function () {
$("#checkbox").change(function ()
{
var n = $(this).is(':checked');
if ($(this).is(':checked')) {
$.ajax({
url: '@Url.Action("Index1", "Check")',
data: { check: n },
type: 'GET',
datatype: "json",
contentType: 'application/json; charset=utf-8',
async: false,
success: function (data) {
alert(data.results);
}
});
}
});
});
</script>
</head>
<body>
<div class="editor-field fieldwidth floatL">
@Html.CheckBoxFor(x => Model.checkCntrl, new { id = "checkbox"})
</div>
</body>
</html>
控制器中的代码:
using checkbox_app.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace checkbox_app.Controllers
{
public class CheckController : Controller
{
//
// GET: /Check/
public ActionResult Index()
{
return View();
}
public ActionResult Index1(bool check)
{
if (check)
{
string str = "done";
return Json(new { results = str }, JsonRequestBehavior.AllowGet);
}
else
{
return View("Error");
}
}
}
}
是否将复选框绑定到属性?不,我没有..我只想检查该复选框是否在控制器中被选中。多了解一些上下文会很有用。您当前的视图、模型和控制器是什么样子的?在较高的层次上,您需要将复选框的值绑定到模型上的属性,但如何最好地做到这一点需要更多的上下文。
using checkbox_app.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace checkbox_app.Controllers
{
public class CheckController : Controller
{
//
// GET: /Check/
public ActionResult Index()
{
return View();
}
public ActionResult Index1(bool check)
{
if (check)
{
string str = "done";
return Json(new { results = str }, JsonRequestBehavior.AllowGet);
}
else
{
return View("Error");
}
}
}
}