在 WebApp 中正确使用带有模型的 c# razor Checkbox
Proper use of c# razor Checkbox with Model in WebApp
我在我的 c# WebApp 中创建运行良好的复选框时遇到问题。
谁能告诉我一个带有模型的工作版本以及视图和控制器的外观。
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TEST.Models
{
public class Checkbox
{
public bool IsChecked { get; set; }
}
}
查看:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.CheckBox()
}
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace TEST.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
}
这是它的工作原理。
型号:
namespace TEST.Models
{
public class Checkbox
{
public bool IsChecked { get; set; }
}
}
控制器:
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Checkbox model)
{
// work with model.IsChecked
}
}
查看:
@model TEST.Models.Checkbox
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.CheckBoxFor(m => m.IsChecked)
}
我在我的 c# WebApp 中创建运行良好的复选框时遇到问题。 谁能告诉我一个带有模型的工作版本以及视图和控制器的外观。
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TEST.Models
{
public class Checkbox
{
public bool IsChecked { get; set; }
}
}
查看:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.CheckBox()
}
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace TEST.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
}
这是它的工作原理。
型号:
namespace TEST.Models
{
public class Checkbox
{
public bool IsChecked { get; set; }
}
}
控制器:
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Checkbox model)
{
// work with model.IsChecked
}
}
查看:
@model TEST.Models.Checkbox
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.CheckBoxFor(m => m.IsChecked)
}