【问题标题】:Some problems with Checkbox. I can't create a checkbox. (asp.net MVC)复选框的一些问题。我无法创建复选框。 (asp.net MVC)
【发布时间】:2018-07-29 15:17:53
【问题描述】:

该应用程序的目的是为报价添加价格并为报价选择一个或多个日期。我必须借助复选框来选择这些日子。我有两节课,一是offer,一是天。

我的班级报价:

    [Key]
    public int Id{ get; set; }

    public double price{ get; set; }

    public List<Days> days {get; set; }

我的上课日:

    [Key]
    public int Id{ get; set; }

    public string Name { get; set; }

    public bool Select { get; set; }

这是我的控制器:

public ActionResult AddOffer()
    {
        return View();
    }

问题是我无法添加复选框。我试试这个:

public ActionResult AddOffer()
    {
        Days days= new Jours();
        days.Id= 1;
        days.Name= "Monday";
        days.Select = false;
        return View(days);
    }

它不起作用,因为它说“参数类型 Days 不能分配给模型类型 Offer。事实上,在我看来,我使用 @model Project.Models.Offer。但我需要它。所以我不知道如何添加复选框有这个约束。

【问题讨论】:

    标签: c# asp.net-mvc visual-studio


    【解决方案1】:

    请按照此示例使用 Data Annotations 属性:

    ViewModel 类:

    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace MvcRequiredCheckbox
    {
        public class SampleViewModel
        {
            [Display(Name = "Terms and Conditions")]
            [Range(typeof(bool), "true", "true", ErrorMessage = "You gotta tick the box!")]
            public bool TermsAndConditions { get; set; }    
        }
    }
    

    控制器类:

    namespace MvcRequiredCheckbox
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Index(SampleViewModel viewModel)
            {
                if(!ModelState.IsValid)
                {
                    return View(viewModel);
                }
    
                return Content("Success");
            }
        }
    }
    

    查看:

    @model MvcRequiredCheckbox.SampleViewModel
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>ASP.NET MVC - Required Checkbox with Data Annotations</title>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        <style type="text/css">
            .field-validation-error {
                color: #ff0000;
                display: block;
            }
        </style>
    </head>
    
    <body>
        <div class="container">
            <div class="col-md-6 col-md-offset-3">
                <h1>ASP.NET MVC - Required Checkbox with Data Annotations</h1>
    
                @using (Html.BeginForm())
                {
                    <div class="form-group">
                        @Html.CheckBoxFor(x => x.TermsAndConditions) 
                        @Html.LabelFor(x => x.TermsAndConditions)
                        @Html.ValidationMessageFor(x => x.TermsAndConditions)
                    </div>
    
                    <button type="submit" class="btn btn-success submit">Submit</button>
                }
            </div>
        </div>
        <hr />
        <div class="credits text-center">
          <p>
              <a href="http://jasonwatmore.com/post/2013/10/16/ASPNET-MVC-Required-Checkbox-with-Data-Annotations.aspx">ASP.NET MVC - Required Checkbox with Data Annotations</a>
          </p>
          <p>
            <a href="http://jasonwatmore.com">JasonWatmore.com</a>
          </p>
        </div>
    
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
        <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js"></script>
        <script src="//ajax.aspnetcdn.com/ajax/mvc/5.2.3/jquery.validate.unobtrusive.js"></script>
    
        <script>
            // extend jquery range validator to work for required checkboxes
            var defaultRangeValidator = $.validator.methods.range;
            $.validator.methods.range = function(value, element, param) {
                if(element.type === 'checkbox') {
                    // if it's a checkbox return true if it is checked
                    return element.checked;
                } else {
                    // otherwise run the default validation function
                    return defaultRangeValidator.call(this, value, element, param);
                }
            }
        </script>
    </body>
    

    发件人:https://dotnetfiddle.net/JbPh0X

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 2011-10-30
      • 2010-10-30
      • 2013-02-19
      • 2011-01-05
      • 2011-08-03
      相关资源
      最近更新 更多