ModelState.IsValid返回False即使没有错误为什么?

时间:2017-08-09 10:28:03

标签: c# asp.net asp.net-mvc-4

我阅读了很多关于if(ModelState.IsValid)的问题,但是错误地返回false,我也尝试了这段代码:

var errors = ModelState.Values.SelectMany(v => v.Errors);

找到什么错误,但它没有显示任何错误。 但是if(ModelState.Isvalid)总是返回false。我不知道我在模型,控制器或视图中做了什么错误。

型号:

public class AdminSignup
{
    [Required(ErrorMessage="Contact Person is Required",AllowEmptyStrings=false)]
    public string ContactPerson { get; set; }

    [Required(ErrorMessage="Email Id is Required",AllowEmptyStrings=false)]
    public string EmailId { get; set; }

    [Required(ErrorMessage="User Name is Required",AllowEmptyStrings=false)]
    public string UserName { get; set; }

    [Required(ErrorMessage="Password is Required",AllowEmptyStrings=false)]
    public string Password { get; set; }

    [Required(ErrorMessage="Apartment Name is Required",AllowEmptyStrings=false)]
    public string ApartmentName { get; set; }

    [Required(ErrorMessage="Location/City is Required",AllowEmptyStrings=false)]
    public string LocationCity { get; set; }

    [Required(ErrorMessage="Apartment Address is Required",AllowEmptyStrings=false)]
    public string ApartmentAddress { get; set; }

    [Required(ErrorMessage="Apartment PhoneNumber is Required",AllowEmptyStrings=false)]
    public string ApartmentPhoneNumber { get; set; }

    [Required(ErrorMessage="NoOfUnits is Required",AllowEmptyStrings=false)]
    public string NoOfUnits { get; set; }     
}

控制器:

[HttpPost]        
public ActionResult signup(AdminSignup asign)
{
    if ( ModelState.IsValid)
    {                           
        //SqlConnection con = new SqlConnection(connString);
        //con.Open();
        //SqlCommand cmd = new SqlCommand("Insert into AdminSignup (ApartmentName,ContactPerson,LocationCity,ApartmentAddress,ApartmentPhoneNumber,EmailId,NoOfUnits)  values('" + asign.ApartmentName + "','" + asign.ContactPerson + "','" + asign.LocationCity + "','" + asign.ApartmentAddress + "','" + asign.ApartmentPhoneNumber + "','" + asign.EmailId + "','" + asign.NoOfUnits + "')", con);
        //cmd.ExecuteNonQuery();

        //AdminSignup adminsignup = new AdminSignup
        //{
        //    EmailId = asign.EmailId
        //};

        //TempData["EmailId"] = adminsignup;

        //con.Close();
        return RedirectToAction("SignupStep2");
    }
    return View();    
}

观看:

@using (Html.BeginForm("signup", "Apartment", FormMethod.Post, new { @class = "form-horizontal", @Id = "first-form" }))
{
  //@Html.ValidationSummary(false)
  <div class="box-body">
    <div class="form-group">
      <label for="apartment_name" class="col-sm-3 control-label">Apartment Name &nbsp; <sup>*</sup></label>
      <div class="col-sm-9">
        @* <input class="form-control" id="apartment_name" placeholder="Apartment Name" type="text" name="apartment_name">*@
        @Html.TextBoxFor(m => m.ApartmentName, new {@class="form-control",@Id="ApartmentName",placeholder="Apartment Name" })   
        @Html.ValidationMessageFor(m=>m.ApartmentName)                           
      </div>
    </div>
    <div class="form-group">
      <label for="contact_person" class="col-sm-3 control-label">Contact Person &nbsp; <sup>*</sup></label>
      <div class="col-sm-9">
        @*<input class="form-control" id="contact_person" placeholder="Password" type="text" name="contact_person">*@
        @Html.TextBoxFor(m => m.ContactPerson, new {@class="form-control",@Id="ContactPerson",placeholder="Contact Person" })
        @Html.ValidationMessageFor(m=>m.ContactPerson)
      </div>
    </div>            
    <div class="form-group">
      <label for="loc_city" class="col-sm-3 control-label">Location / City &nbsp; <sup>*</sup></label>
      <div class="col-sm-9">
        @* <input class="form-control" id="loc_city" placeholder="Location / City" type="text" name="loc_city">*@
        @Html.TextBoxFor(m => m.LocationCity, new {@class="form-control",@Id="LocationCity",placeholder="Location/City" })
        @Html.ValidationMessageFor(m=>m.LocationCity)
      </div>
    </div>
    <div class="form-group">
      <label for="apt_address" class="col-sm-3 control-label">Apartment Address &nbsp; <sup>*</sup></label>
      <div class="col-sm-9">
        @*<textarea name="apt_address" id="" cols="10" rows="5" class="form-control">Apartment Address</textarea>*@
        @Html.TextAreaFor(m => m.ApartmentAddress, new {@class="form-control",@Id="ApartmentAddress" ,placeholder="Apartment Address" })
        @Html.ValidationMessageFor(m=>m.ApartmentAddress)
      </div>
    </div>
    <div class="form-group">
      <label for="apt_number" class="col-sm-3 control-label">Apartment PhoneNumber &nbsp; <sup>*</sup></label>
      <div class="col-sm-9">
        @*<input class="form-control" id="apt_number" placeholder="Apartment Number" type="text" name="apt_number">*@
        @Html.TextBoxFor(m => m.ApartmentPhoneNumber, new {@class="form-control",@Id="ApartmentPhoneNumber",placeholder="Apartment PhoneNumber" })
        @Html.ValidationMessageFor(m=>m.ApartmentPhoneNumber)
      </div>
    </div>
    <div class="form-group">
      <label for="apt_emailid" class="col-sm-3 control-lable">Email Id &nbsp; <sup>*</sup></label>
      <div class="col-sm-9">
        @Html.TextBoxFor(m => m.EmailId, new {@class="form-control",@Id="Email Id",placeholder="EmailId" })
        @Html.ValidationMessageFor(m=>m.EmailId)
      </div>
    </div>
    <div class="form-group">
      <label for="apt_units" class="col-sm-3 control-label">No of Units &nbsp; <sup>*</sup></label>
      <div class="col-sm-9">
        @*<input class="form-control" id="apt_units" placeholder="No of Units" type="text" name="apt_units">*@
        @Html.TextBoxFor(m => m.NoOfUnits, new {@class="form-control",@Id="NoOfUnits",placeholder="No.Of.Units" })
        @Html.ValidationMessageFor(m=>m.NoOfUnits)
      </div>
    </div>
    <div class="form-group text-center">
      <input type="submit" value="Next" name="first_form" class="btn btn-info" id="btn_first"/>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

再次查看您的视图和模型,您已在class内的所有属性上添加了必需项,但是您将返回7 of 9属性。然后Model.IsValid将查看您的Model并查看哦2 required properties您不是returning,因此它将是错误的。要么删除这些属性上的[required],要么在视图中添加它们。祝你好运。

<强>更新 这两个属性是:

UserNamePassword,它们必须位于您的Html.BeginForm内,因此它们将作为您班级的一部分发送到服务器。