'必需'没有解雇MVC

时间:2017-02-21 10:45:27

标签: c# asp.net-mvc

型号:

 @extends ('layouts/master')

    @section('title', 'Allalaadimine')

        @section ('content')
        <div class="container medicine_confirmation_container">
          <div class="col-md-12">
            @foreach ($formData as $data)
              <li>{{$data}}</li>
            @endforeach
            <div class="flash-message">
              @if(session()->has('message'))
                  <div class="alert-message alert-message-success">
                    <h4>Saved!</h4>
                      {{ session()->get('message') }}
                  </div>
              @endif
            </div> <!-- end .flash-message -->
            <button class="btn btn-borders btn-success btn-lg btnDownloadPdf" type="submit">Lae alla pdf</button>
          </div>
        </div>
        @endsection

        @section ('footer')
        @endsection

控制器:

[Required(ErrorMessage = "Username Required")]
  [StringLength(30, ErrorMessage = ":Less than 30 characters")]
  public string name { get; set; }
  public string Email { get; set; }
  public string mob { get; set; }

查看:

   [HttpPost]
    public ActionResult Insert(Employee emp)
    {
        Employee emp1 = new Employee();
        emp1.insert(emp);
        return View();
    }

当我点击提交时,名称空值不显示“用户名必填”。提前致谢。

1 个答案:

答案 0 :(得分:2)

你缺少一些东西

1)在视图中,您应添加@Html.ValidationSummary以显示所有验证错误,或者您可以使用@Html.ValidationMessageFor(m => m.name)显示特定属性的错误消息。

2)在控制器中添加

 [HttpPost]
    public ActionResult Insert(Employee emp)
     {

      if(!ModelState.IsValid)

        {
          return view(emp)
        }

        Employee emp1 = new Employee();
        emp1.insert(emp);
        return View();
     }

检查模态是否无效,然后返回当前页面并显示验证错误