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);
}
Related
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;
}
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
}
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.
I have read some posts, but I still find it difficult to solve this. My problem is that my action only reads some of the values from the binded list. This is how I send the list to the view:
public ActionResult RegisterSurvey()
{
RegisterSurveyModel model = new RegisterSurveyModel();
var questions = new List<QuestionModel>();
var survey = EFSurvey.Survey.FirstOrDefault();
survey.QuestionSurvey
.Where(x => x.AuditingDeleted == false)
.Where(x => x.Active == true).ToList().ForEach((item) =>
{
var questionModel = new QuestionModel();
ModelCopier.CopyModel(item, questionModel);
questionModel.Answer = string.Empty;
questions.Add(questionModel);
});
model.Questions = questions;
return View(model);
}
This is my model:
public class RegisterSurveyModel
{
public List<QuestionModel> Questions { get; set; }
}
public class QuestionModel
{
public int QuestionSurveyID { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
public bool Suggestion { get; set; }
}
This is my view:
<div class="SiteSurveyContainer">
#using (Html.BeginForm())
{
<div class="SurveyUp">
#for (int i = 0; i < Model.Questions.Count(); i++)
{
if (!Model.Questions[i].Suggestion)
{
<p>#Model.Questions[i].Question</p>
#Html.HiddenFor(x => Model.Questions[i].QuestionSurveyID);
#Html.TextBoxFor(x => Model.Questions[i].Answer, new { #class = "surveyBox" });
}
}
</div>
<div class="SurveyBottom">
<div class="line">
</div>
<p>
Suggestions</p>
#for (int i = 0; i < Model.Questions.Where(x => x.Suggestion == true).Count(); i++)
{
#Html.HiddenFor(x => Model.Questions[i].QuestionSurveyID);
#Html.TextAreaFor(x => Model.Questions[i].Answer, new { #class = "surveyTextArea" })
}
</div>
<div class="surveyBottomButton">
<input type="submit" value="Submit Results" />
</div>
}
So far so good. Anyway, when I answer all of the survey questions, I only get the first 4 answers... Weird. Anyone knows why is that happening?
You have multiple input controls with within the same form with the same name.
Suggested questions are a sub set of all questions, so they are being repeated twice on the same form. That will throw off the ModelBinder, so your receiving action is probably only seeing the questions that have not been repeated.
I'm working on a project using MVC3. I've first created a database (with the necessary constraints like PK and FK's) and added this database to my webapplicatie using the ADO.NET Entity Data Model. Everything seems to work fine but I can't make validation flexible. Here some code to clarify my question.
Edit: Added the View + correct models. This version is without importing a database but results the same.
--Models----
namespace CustomValidation.Models
{
public class Person : IValidatableObject
{
public int Id { get; set; }
[Required]
public String FirstName { get; set; }
[Required]
public String LastName { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (String.IsNullOrEmpty(FirstName))
{
var field = new[] { "FirstName" };
yield return new ValidationResult("Firstname can't be null", field);
}
}
}
}
namespace CustomValidation.Models
{
public class Address : IValidatableObject
{
public int Id { get; set; }
[Required]
public String Streetname { get; set; }
[Required]
public String Zipcode { get; set; }
[Required]
public String City { get; set; }
[Required]
public String Country { get; set; }
public Person Person { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (String.IsNullOrEmpty(Streetname))
{
var field = new[] { "Streetname" };
yield return new ValidationResult("Streetname can't be null", field);
}
}
}
}
public class MasterModel
{
public List<Address> Addressess { get; set; }
public Person Person { get; set; }
}
}
namespace CustomValidation.Models
{
public class DBEntities : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Address> Addressess { get; set; }
}
}
----Controller----
namespace CustomValidation.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
MasterModel Model = new MasterModel();
Model.Person = new Person();
Model.Addressess = new List<Address>();
Model.Addressess.Add(new Address());
Model.Addressess.Add(new Address());
return View(Model);
}
[HttpPost]
public ActionResult Index(MasterModel Model)
{
DBEntities _db = new DBEntities();
if (TryValidateModel(Model.Person))
{
_db.People.Add(Model.Person);
_db.SaveChanges();
}
else
{
return View(Model);
}
for (int i = 0; i < Model.Addressess.Count; i++)
{
if (!String.IsNullOrEmpty(Model.Addressess[i].Country))
{
if (TryValidateModel(Model.Addressess[i]))
{
Model.Addressess[i].Person = Model.Person;
_db.Addressess.Add(Model.Addressess[i]);
_db.SaveChanges();
}
else
{
return View(Model);
}
}
}
return RedirectToAction("Index");
}
}
Here is my View:
#model CustomValidation.Models.MasterModel
#{
ViewBag.Title = "Index";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
<div class="editor-label">
#Html.LabelFor(m => Model.Person.FirstName)
#Html.EditorFor(m => Model.Person.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(m => Model.Person.LastName)
#Html.EditorFor(m => Model.Person.LastName)
</div>
</fieldset>
for (int i = 0; i < Model.Addressess.Count; i++)
{
<fieldset>
<legend>Address</legend>
<div class="editor-label">
#Html.LabelFor(model => Model.Addressess[i].Streetname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Addressess[i].Streetname)
#Html.ValidationMessageFor(model => Model.Addressess[i].Streetname)
</div>
<div class="editor-label">
#Html.LabelFor(model => Model.Addressess[i].Zipcode)
</div>
<div class="editor-field">
#Html.EditorFor(model => Model.Addressess[i].Zipcode)
#Html.ValidationMessageFor(model => Model.Addressess[i].Zipcode)
</div>
<div class="editor-label">
#Html.LabelFor(model => Model.Addressess[i].City)
</div>
<div class="editor-field">
#Html.EditorFor(model => Model.Addressess[i].City)
#Html.ValidationMessageFor(model => Model.Addressess[i].City)
</div>
<div class="editor-label">
#Html.LabelFor(model => Model.Addressess[i].Country)
</div>
<div class="editor-field">
#Html.EditorFor(model => Model.Addressess[i].Country)
#Html.ValidationMessageFor(model => Model.Addressess[i].Country)
</div>
</fieldset>
}
<p>
<input type="submit" value="Create" />
</p>
}
The problem (that I've noticed while debugging something like this) is that before the request gets inside of my action MVC first runs validators for your collection. So if I've only filled out Person in my form, while validating Model.Person it just return false (i guess because of the Required fields for Address) and so it will bounce back to my View.
I need to have some workaround that lets me decided which instances of my collection I want to validate in my action.
I've not been able to find any solution after a lot of time searching on the net.
I hope that someone can help me out.
PS. I'm quite a newbie with Visual Studio and MVC 3 so please don't be to hard on me :)
In order to validate sub-models of the model returned to your action, you need to apply TryValidateModel to the specific sub-model, and use the overload which includes the prefix for the model. If you look at your generated HTML, you will see that the Person fields have Person as a prefix, the first Address fields have Addressess[0] as the prefix, and the second Address fields have Addressess[1] as the prefix.
In addition, you must clear all model errors before invoking TryValidateModel (or the existing errors will cause TryValidateModel to return false).
Do the following:
[HttpPost]
public ActionResult Index(MasterModel model)
{
var reloadView = true;
// clear any existing errors
foreach (var key in ModelState.Keys)
ModelState[key].Errors.Clear();
if (TryValidateModel(model.Person, "Person"))
{
_db.People.Add(model.Person);
_db.SaveChanges();
reloadView = false;
for (var i = 0; i < model.Addressess.Count(); i++)
{
foreach (var key in ModelState.Keys)
ModelState[key].Errors.Clear();
if (TryValidateModel(model.Addressess[i], "Addressess[" + i.ToString() + "]"))
{
**// UPDATED CODE**
// add Person to Address model
model.Addressess[i].Person = model.Person;
_db.Addressess.Add(model.Addressess[i]);
_db.SaveChanges();
}
}
}
if (reloadView)
{
// clear and re-add all errors
foreach (var key in ModelState.Keys)
ModelState[key].Errors.Clear();
TryValidateModel(model);
return View(model);
}
else
{
// go to some other view
}
}