Validations not working in MVC3? - asp.net-mvc-3

This is how my model looks
public class ProjectModel
{
public static List<ProjectModel> GetList { get; set; }
public Int16 ID { get; set; }
[Required(ErrorMessage = "ProjectName is required")]
public string projectName { get; set; }
[Required(ErrorMessage = "Description is required")]
public string Description { get; set; }
[Required(ErrorMessage = "Status is required")]
public string status { get; set; }
}
This is how Controller Looks
#region Insert New Project
//
//View for adding new Projects/
//
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()
{
ViewBag.Status = new SelectList(GetStatus(), "Status", "Status");
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(ProjectModel model,string Status)
{
// var modelList = new List<ProjectModel>();
using (SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True"))
{
conn.Open();
SqlCommand insertcommande = new SqlCommand("Sp_AddNewProject", conn);
insertcommande.CommandType = CommandType.StoredProcedure;
insertcommande.Parameters.Add("#ProjectName", SqlDbType.VarChar).Value = model.projectName;
insertcommande.Parameters.Add("#Description", SqlDbType.VarChar).Value = model.Description;
insertcommande.Parameters.Add("#Status", SqlDbType.VarChar).Value =Status;
insertcommande.ExecuteNonQuery();
}
return RedirectToAction("Create");
}
#region To Edit th Existing Project Record
//
//View For displaying the record to be edited/
//
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Edit(int id, ProjectModel updatemodel)
{
ViewBag.Status = new SelectList(GetStatus(), "Status", "Status");
SqlConnection cn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select ProjectId,projectName,Description,status From Projects Where ProjectId=" + id, cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
//if ( dr[0]) != DBNull.Value)
updatemodel.ID = Convert.ToInt16(dr["ProjectId"]);
updatemodel.projectName = dr["projectName"].ToString();
updatemodel.Description = dr["Description"].ToString();
updatemodel.status = dr["status"].ToString();
}
else
{
dr.Close();
}
dr.Close();
cn.Close();
return View(updatemodel);
}
//
//Action for editing the record which is in view/
//
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(ProjectModel updatemodel, FormCollection form, int id,string Status)
{
SqlConnection cn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True");
SqlCommand cmd = new SqlCommand("UpdateProject", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
cmd.Parameters.Add("ProjectId", SqlDbType.Int).Value = updatemodel.ID;
cmd.Parameters.Add("projectName", SqlDbType.VarChar).Value = updatemodel.projectName;
cmd.Parameters.Add("Description", SqlDbType.VarChar).Value = updatemodel.Description;
cmd.Parameters.Add("status", SqlDbType.VarChar).Value = Status;
cn.Close();
return View(updatemodel);
}
#endregion
and this is how my aspx page looks
<% using (Html.BeginForm())
{ %>
<%-- <form action="Create.aspx" method="post"></form>--%>
<%:Html.ValidationSummary(true)%>
<fieldset>
<legend style="color:Orange; font-weight:bolder;">AddNew Project</legend>
<div class="editor-label" style="color:Orange; font-weight:bolder;">
<%: Html.LabelFor(model => model.projectName)%>
</div>
<div class="editor-field">
<%:Html.EditorFor(model => model.projectName)%>
<%: Html.ValidationMessageFor(model => model.projectName)%>
</div>
<div class="editor-label" style="color:Orange; font-weight:bolder;">
<%:Html.LabelFor(model => model.Description)%>
</div>
<div class="editor-field">
<%:Html.EditorFor(model => model.Description)%>
<%:Html.ValidationMessageFor(model => model.Description)%>
</div>
<div class="editor-label" style="color:Orange; font-weight:bolder;">
<%:Html.LabelFor(model => model.status)%>
</div>
<div class="editor-field">
<%:Html.DropDownList("Status")%>
<%:Html.ValidationMessageFor(model => model.status)%>
</div>
<p>
<input type="submit" value="Create" style="color:Orange; font-weight:bolder;"/>
</p>
</fieldset>
<%} %>
My problem is Validations are not fired In my Create new Page when i click submit button
but validations are geting before even i cilck in my edit page can any one tell me where am i doing wrong
or there is any other method to provide validations

You aren't actually checking to see if any validations have failed. In your controller methods where you post, you need to have a check:
if (ModelState.IsValid) {
// do your work
return RedirectToAction("WhereYouWantToGoAfterwards");
}
return View(model);
EDIT:
As I said above, you're not actually checking for errors in your code. However, the effect you are seeing that is different between the Create and Edit is that in the Edit, you return a View(), but in the Create you RedirectToAction. Redirect's clear the ModelState, so you would not see any errors.
You still need to check if the ModelState.IsValid before you start doing work based on the data. Otherwise, you're going to run into a lot of trouble.

Related

How to create drop down list in mvc

This is my controller
[AllowAnonymous]
public ActionResult Register()
{
ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
return View();
}
This is my Create view part
<div class="form-group">
#Html.Label("Select Your User Type", new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#*#Html.DropDownList("Name")*#
#Html.DropDownList("Name",(SelectList)ViewBag.Name )
</div>
</div>
i can load the form with drop down. but when I am try to save the record it gives an error "The ViewData item that has the key 'Name' is of type 'System.String' but must be of type 'IEnumerable'."
Do this:
[AllowAnonymous]
public ActionResult Register()
{
var items = new List<SelectListItem>();
foreach (var role in context.Roles)
{
items.Add(new SelectListItem {Text = role.Name, Value = role.Value});
}
var result = new SelectList(items);
ViewBag.Name = result;
return View();
}
View
#Html.DropDownList("Name",ViewBag.Name)
But can I suggest that you avoid ViewBag and use strongly typed models.
Look here

how can i get dropdownlist id in controller when we select an item using mvc4

I have dropdownlst which i have filled from database. Now i need to get the selected value in Controller do some manipulation. But null data will get. Code which i have tried.
##View##
#using (Html.BeginForm()) {
#Html.DropDownList("SupplierId", ViewBag.PurSupName as SelectList, new {#class = "form-control",style = "width: 200px;",id="supId"})
}
## Controller ##
public ActionResult ItemPurchased()
{
POS_DBEntities2 db = new POS_DBEntities2();
var query = from i in db.TBL_Account
where i.Type == "supplier"
select i;
ViewBag.PurSupName = new SelectList(query, "AccountId", "AccountName");
return PartialView("ItemPurchased", new List<PurchasedItem>());
}
##Controller##
public ActionResult GetPurchasedItem()
{
var optionsValue = this.Request.Form.Get("SupplierId");
return View();
}
You can try with below changes.
#using (Html.BeginForm("GetPurchasedItem","YourController")) {
#Html.DropDownList("SupplierId", ViewBag.PurSupName as SelectList, new {#class = "form-control",style = "width: 200px;",id="supId"})
<input type="submit" value="OK" />
}
Controller will be
[HttpPost]
public ActionResult GetPurchasedItem(string SupplierId)
{
var optionsValue = SupplierId;
//..............
return View();
}
It should work normally..

Adding items to list in model dynamically via ajax

I have the following code and I am trying to add the selected value to the model.SelectedPayCodes list everytime the user makes a selection from the dropdown and clicks on "Add"
CONTROLLER
[HttpPost]
public PartialViewResult AddPayCode(ReferralModel model, string SelectedPayCode)
{
var payCode = _employeeService.GetPayCodeById(Convert.ToInt32(SelectedPayCode));
model.Add(payCode);
return PartialView("_PayCodesPartial",model);
}
MODEL
public class ReferralModel
{
public Customer Customer { get; set; }
public Employee Employee { get; set; }
public List<PayCode> PayCodes { get; set; } // List of paycodes
public List<PayCode> PayCodesList { get; set; } // List of 'selected' paycodes
public SelectListItem SelectedPayCode { get; set; } // current selected paycode
public Referral Referral { get; set; }
public ReferralModel()
{
PayCodesList = new List<PayCode>();
}
// Step 3 - Add Paycode is done here.
public void Add(PayCode payCode)
{
PayCodesList.Add(payCode);
}
}
VIEW - Create.cshtml
#using (Ajax.BeginForm("AddPayCode", "Referral",
new AjaxOptions()
{
HttpMethod = "POST",
InsertionMode = InsertionMode.InsertAfter,
UpdateTargetId = "PayCodes",
}))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary()
<!-- payCodes input-->
<div class="control-group col-lg-6">
<label class="control-label">Product</label>
<div class="controls">
<!-- Step 1 + Choose PayCode Here -->
#Html.DropDownListFor(model => model.SelectedPayCode, new SelectList(Model.PayCodes.ToList(), "Id", "Description"), "- Select -")
<input type="submit" value="Add" />
</div>
</div>
<!-- pay codes list -->
<div id="PayCodes">
</div>
}
VIEW - _PayCodesPartial.cshtml
#model Zenwire.Models.ReferralModel
#if (Model.PayCodesList != null)
{
foreach (var item in Model.PayCodesList)
{
string.Format("{0} - {1}", item.Code, item.Description);
}
}
The issue seems to be whenever a new item is added the list is empty again and does not retain it's previous selection.
Try this. I used Session State
public PartialViewResult AddPayCode(ReferralModel model, string SelectedPayCode)
{
var payCode = _employeeService.GetPayCodeById(Convert.ToInt32(SelectedPayCode));
model.PayCodesList = GetPayCodes();
model.Add(payCode);
Session["paycodes"] = model.PayCodesList;
return PartialView("_PayCodesPartial",model);
}
private List<PayCode> GetPayCodes()
{
List<PayCode> payCodes = (List<PayCode>)Session["paycodes"];
if (payCodes == null )
{
payCodes = new List<PayCode>();
Session["paycodes"] = paycode;
}
return payCodes;
}

MVC4 Creating Register pages using one Model

I don't really have a good example for this mostly because I'm just winging it and so far, my code is just a jumbled up mish-mash of gobble-dee-gook. I'm at a loss and need some help or advice on this one. Here's what I need:
I am creating a mock register form meant for learning purposes only. I used a job application form as my example. One page has the applicant's personal info such as first and last name, age, gender, and level of education. The second page allows them to choose which position they wish to apply for and allows them to provide a list of skills. I have a single model set up to take the data and save it to a database. The first page will have an ajax next button that replaces the first page form with the second page form. The second page has two buttons, Back and Submit (simple enough) that are also ajax-y. My issue right now is getting the data from both forms to save to a single entry in the model. Does anyone have a simple example or a link I could look into for this kind of situation? Or maybe even another way of going about this? It would be greatly appreciated!! :)
Model
public int Id { get; set; }
[Required(ErrorMessage = "First name Req")]
[Display(Name = "First name")]
public string First { get; set; }
[Required(ErrorMessage = "Last name Req")]
[Display(Name = "Last name")]
public string Last { get; set; }
[Required(ErrorMessage = "Age Req")]
[Range(18, 75, ErrorMessage = "Age Range of {1}-{2}")]
public int Age { get; set; }
[Required(ErrorMessage = "Please Select Gender")]
public string Gender { get; set; }
[Required(ErrorMessage = "Education Level Req")]
public string Education { get; set; }
public string Position { get; set; }
public string Skills { get; set; }
Controller
[HttpGet]
public PartialViewResult Apply1()
{
var model = db.Applications.Find(id);
if (model != null)
{
return PartialView("_Apply1", model);
}
return PartialView("_Apply1");
}
[HttpPost]
public PartialViewResult Apply1(Application app)
{
if (Request.IsAjaxRequest())
{
if (db.Applications.Where(a => a.First.ToLower() == app.First.ToLower() && a.Last.ToLower() == app.Last.ToLower() && a.Age == app.Age && a.Gender == app.Gender && a.Education == app.Education).Count() == 0)
{
app.Position = "x";
db.Applications.Add(app);
db.SaveChanges();
}
else
{
app = db.Applications.Single(a => a.First.ToLower() == app.First.ToLower() && a.Last.ToLower() == app.Last.ToLower() && a.Age == app.Age && a.Gender == app.Gender && a.Education == app.Education);
}
PosList(); //This is a helper Method, get's values for a dropdown list
id = app.Id;
var model = db.Applications.Find(id);
return PartialView("_Apply2", model);
}
return PartialView("_Apply1", app);
}
[HttpGet]
public PartialViewResult Apply2()
{
var model = db.Applications.Find(id);
if (model != null)
{
return PartialView("_Apply2", model);
}
return PartialView("_Apply2");
}
[HttpPost]
public PartialViewResult Apply2(Application app)
{
if (ModelState.IsValid)
{
db.Entry(app).State = EntityState.Modified;
db.SaveChanges();
ModelState.Clear();
return PartialView("_Success");
}
PosList();
return PartialView("_Apply2", app);
}
First View
#model SiPrac.Models.Application
#using (Ajax.BeginForm("Apply1", new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "appForm"
}))
{
#Html.AntiForgeryToken()
<div class="editor-label">
#Html.LabelFor(model => model.First)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.First)
#Html.ValidationMessageFor(model => model.First)
</div>
<div class="clear"></div>
<div class="editor-label">
#Html.LabelFor(model => model.Last)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Last)
#Html.ValidationMessageFor(model => model.Last)
</div>
<div class="clear">
<input class="btn" type="submit" value="Next" />
}
Second View
#using (Ajax.BeginForm("Apply2", new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "appForm"
}))
{
#Html.AntiForgeryToken()
<div class="editor-label">
#Html.LabelFor(model => model.Position)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.Position, (IEnumerable<SelectListItem>)ViewData["selectPos"])
</div>
<div class="clear"></div>
<input class="btn" type="submit" value="Submit" />
}
Okay I'll try to make this as short as possible. So assuming you already know how to switch between forms, as I know you do based on your question, and you have forms like these:
<form id="form1">
#Html.EditorFor(model => model.First)
// rest of the fields goes here
</form>
<form id="form2">
#Html.EditorFor(model => model.Position)
// rest of the fields goes here
<button id="submitAll">Submit</button>
</form>
That assumes that you have the buttons to switch back and forth the views. The submitAll button triggers the postback action to a controller method and pass the value like this:
function submitAllFields() {
var o = {
// list properties here similar to your model property name
// and assign values to them
First: $("#First").val(),
Position: $("#Position").val()
};
$.post('#Url.Action("apply2")', { app: o }, function(result) {
// do something here
});
}
You then need one method to accept all the inputs:
[HttpPost]
public PartialViewResult Apply2(Application app)
{
if (ModelState.IsValid)
{
db.Entry(app).State = EntityState.Modified;
db.SaveChanges();
ModelState.Clear();
return PartialView("_Success");
}
// do something here
}

View "ChangePassword" in MVC 3

Can you help me to build my "ChangePassword" View in MVC3 ?
Here what I have tried to do:
ProfileTeacherController.cs
public ViewResult ChangePassword(int id)
{
var user = User.Identity.Name;
int inter = int.Parse(user);
var teachers = from t in db.Teachers
where t.AffiliationNumber == inter
select t;
Teacher teacher = new Teacher();
foreach (var teach in teachers)
{
teacher = teach;
}
return View(teacher);
}
[HttpPost]
public ActionResult ChangePassword(Teacher teacher)
{
if (ModelState.IsValid)
{
// How can I compare the two fields password in my view ?
db.Entry(teacher).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Edit", "ProfileTeacher", new { id = teacher.TennisClubID });
}
return View(teacher);
}
Here the ChangePassword (View)
#model TennisOnline.Models.Teacher
#{
ViewBag.Title = "ChangePassword";
}
<h2>Changement du mot de passe</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend></legend>
<div class="editor-label">
#Html.Label("Enter the new password")
</div>
<div class="editor-field">
#Html.PasswordFor(model => model.Pin, new { value = Model.Pin })
</div>
<div class="editor-label">
#Html.Label("Confirm your password")
</div>
<div class="editor-field">
#Html.Password("ConfirmPassword")
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
So, How can I verify in my controller if the two passwords are the same, please ? Thanks in advance
In addition you can add the message to be shouwn when the two password are not the same in the Compare attribute.
[Compare("NewPassword", ErrorMessage = "The new password and confirm password do not match.")]
I would recommend the usage of a view model:
public class TeacherViewModel
{
...
[Compare("ConfirmPassword")]
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
now have your view take the view model and also your Post action.
In addition to that in your GET action you seem to have written some foreach loop which I don't see its usage. You could simplify:
[Authorize]
public ViewResult ChangePassword(int id)
{
var user = User.Identity.Name;
int inter = int.Parse(user);
var teacher = db.Teachers.SingleOrDefault(t => t.AffiliationNumber == inter);
return View(teacher);
}

Resources