kehua668 2017-06-20 10:15 采纳率: 25%
浏览 1640
已结题

mvc4 checkbox如何视图与控制器间互相传值

数据结构
表Document
有类似id,title,MachineType,user,dd...等字段
另一个表MachineType
id,name
1 aaa
2 bbb
3 ccc
4 ddd

 List<Models.MachineTypeModel> mts = ViewBag.mtList;

 ...
  <label class="form-label col-xs-3">标题:</label>
                <div class="formControls col-xs-8">
                    @Html.TextBox("Title", "", new { @class = "input-text" })
                    @Html.ValidationMessageFor(model => model.Title)
                    @Html.ValidationMessage("doubleTitle")
                </div>

    @foreach (var c in mts)
                    {
                        <label class="checkbox">
                            <input type="checkbox" name="MachineType" value="@c.id"> @c.MachineTypeName
                        </label>
                    }

页面已取到值,但是如何与控制器间互相传递已选中的值呢
控制器:

  public ActionResult Index()
        {
            //和模型交互,获取所有docmentType list
            List<DTypeModel> dtype = new DocumentBLL().DocumentType_list();
            SelectList list = new SelectList(dtype, "id", "DocumentTypeName", dtype[0].id);
            ViewBag.dtype = list;

            List<MachineTypeModel> mtype = new DocumentBLL().MachineType_list();
            SelectList list_m = new SelectList(mtype, "id", "MachineTypeName", mtype[0].id);
            ViewBag.mtype = list_m;
            ViewBag.mtList = mtype;//checkbox
            return View("DocAdd");
        }

   public ActionResult Creat(DocumentModel objDocument)
        {
            int res = 0;
            res = new DocumentBLL().Document_Creat(objDocument);
            if (res > 0)
            {
                return Content("<script>alert('创建成功!');location.href='" + Url.Action("Index") + "'</script>");
            }
            else
            {
                return Content("<script>alert('创建失败!');location.href='" + Url.Action("Index") + "'</script>");
            }
        }

图片说明

  • 写回答

2条回答

  • threenewbee 2017-06-20 15:40
    关注

    最简单的,每次勾选都拼接成参数,回传一次,控制器split以后查询

    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛