2017-08-09 88 views
-1

我讀了許多關於if(ModelState.IsValid)返回錯誤虛假的問題,我也試過這個代碼: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

我沒有看到你的要求'Password'屬性視圖任何形式的控制(和自其需要,'ModelState'將是無效的) –

+0

或'UserName'! –

+0

對不起,我把它添加到另一個頁面,所以這是錯誤? – santhosh

回答

1

看看你的視圖和模型,你已經穿上內的所有屬性所需class,但是你要返回7 of 9屬性。然後Model.IsValid會看你的Model,看看哦,看有2 required properties你是不是returning,所以這將是錯誤的。要麼刪除這些屬性上的[required],要麼將它們添加到您的視圖中。祝你好運。

更新 這2個屬性是:

UserName而且Password,他們需要的是你的Html.BeginForm內,所以他們將被髮送到服務器類的一部分。

+0

我把這些用戶名和密碼另一種觀點認爲,這樣將是錯誤,我刪除它,現在它返回真實感謝您的幫助:) – santhosh

+0

@santhosh歡迎您:) – Valkyrie

相關問題