c# - Update few changed fields MVC 3 project -
i making mvc 3 website allows users register , buy stuff online. i'm using ef db operations. implementation user class follows
public class user { [hiddeninput(displayvalue = false)] public long userid { get; set; } [required(errormessage = "please enter first name")] [display(name="first name")] public string firstname { get; set; } [required(errormessage = "please enter last name")] [display(name="last name")] public string lastname { get; set; } [required(errormessage = "please enter email")] [regularexpression(".+\\@.+\\..+", errormessage="please enter valid email")] [display(name = "email")] public string email { get; set; } [required(errormessage = "please enter phone number")] [display(name = "phone number")] public string phonenumber { get; set; } [required(errormessage = "please enter value address line 1")] [display(name = "address line 1")] public string addressline1 { get; set; } [required(errormessage = "please enter value address line 2")] [display(name = "address line 2")] public string addressline2 { get; set; } [required(errormessage = "please enter city")] [display(name = "city")] public string city { get; set; } [required(errormessage = "please enter country")] [display(name = "country")] public string country { get; set; } [required(errormessage = "please enter login name")] [display(name = "your login name")] public string username { get; set; } [required(errormessage = "please enter password")] [datatype(datatype.password)] [compare("password")] [display(name = "your password")] public string password { get; set; } [datatype(datatype.password)] [compare("password", errormessage="passwords not match")] [system.componentmodel.dataannotations.schema.notmapped] [display(name = "confirm password")] public string confirmpassword { get; set; } }
now problem i'm facing while updating user details. want present 2 different pages updating details. 1 changing password , other changing other details. but, problem when i'm creating view enable edit other details, validations password fire , don't allow me save data. want data fields, have not been displayed, same ones stored in db.
please me in this, also, since first question here, please tell me else need solve problem. below code user controller edit action:
public viewresult edit(int userid) { user user = repository.users.where(x => x.userid == userid).select(x => x).firstordefault(); user.username = repository.users.where(x => x.userid == userid).select(x => x.username).firstordefault(); return view(user); } [httppost] public actionresult edit(user user) { //user.password = repository.users.where(u => u.username.equals(user.username)).select(p => p.password).firstordefault().tostring(); user.confirmpassword = user.password; if (modelstate.isvalid) { repository.adduser(user, "edit"); tempdata["message"] = string.format("{0} has been saved", (user.firstname+ " " +user.lastname)); return redirecttoaction("listusers", "user"); } else { return view(user); } }
here code view editing user
@model sportsstore.domain.entities.user @{ viewbag.title = "edit user"; layout = "~/views/shared/_adminlayout.cshtml"; } <h2>edit @model.firstname @model.lastname</h2> @html.validationsummary() @using (html.beginform("edit", "user", formmethod.post)) { <table class="grid"> <tr> <td>first name</td> <td>@html.editorfor(x => x.firstname)</td> </tr> <tr> <td>last name</td> <td>@html.editorfor(x => x.lastname)</td> </tr> <tr> <td>email</td> <td>@html.editorfor(x => x.email)</td> </tr> <tr> <td>phone number</td> <td>@html.editorfor(x => x.phonenumber)</td> </tr> <tr> <td>address line 1</td> <td>@html.editorfor(x => x.addressline1)</td> </tr> <tr> <td>address line 2</td> <td>@html.editorfor(x => x.addressline2)</td> </tr> <tr> <td>city</td> <td>@html.editorfor(x => x.city)</td> </tr> <tr> <td>country</td> <td>@html.editorfor(x => x.country)</td> </tr> </table> @*html.editorfor(x => x.username) @html.passwordfor(x => x.password) @html.hiddenfor(x => x.confirmpassword) @*{model.confirmpassword = model.password;}*@ <input type="submit" value="save" /> if (user.identity.name.equals("admin", stringcomparison.ordinalignorecase)) { @html.actionlink("cancel , return user list", "listusers"); } else { @html.actionlink("cancel , return user list", "list", "product"); } }
you should split viewmodels
, 1 containing user login information, userloginviewmodel
, userinfoviewmodel
, containing other user information. add each model required properties update, exemple userloginmodelo
below:
public class userloginviewmodel { [hiddeninput(displayvalue = false)] public long userid { get; set; } [required(errormessage = "please enter password")] [datatype(datatype.password)] [compare("password")] [display(name = "your password")] public string password { get; set; } [datatype(datatype.password)] [compare("password", errormessage="passwords not match")] [system.componentmodel.dataannotations.schema.notmapped] [display(name = "confirm password")] public string confirmpassword { get; set; } }
then, create typed views based on models. controllers should expect each view model, can map every single atributte real user object , save changes:
view:
@model sportsstore.domain.entities.userloginviewmodel
controller:
[httppost] public actionresult edit(userloginviewmodel user) { //retrieve user , map every updated property original user } [httppost] public actionresult edit(userinfoviewmodel user) { //retrieve user , map every updated property original user }
Comments
Post a Comment