Reputation: 107
I read many of the questions about if(ModelState.IsValid)
returns false with error, I also tried this code:
var errors = ModelState.Values.SelectMany(v => v.Errors);
to find what error but it shows no error in it.
But if(ModelState.Isvalid)
returns always false. I don't known what mistake I've done in Model, Controller or View.
Model:
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; }
}
Controller:
[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();
}
And 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 <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 <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 <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 <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 <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 <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 <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>
Upvotes: 0
Views: 1535
Reputation: 3228
Look at your view and model again, you have put required on all your properties within the class
, however you are returning 7 of 9
properties. then Model.IsValid
will look at your Model
and see oh look there 2 required properties
that you are not returning
, so it will be false. either remove [required]
on those properties or add them within your view. good luck.
Update Those 2 Properties are:
UserName
And Password
, they need to be within your Html.BeginForm
, so they will be send to server as the part of your class.
Upvotes: 3