asp.net mvc - How to input data to custom column in userprofile table when creating a new user -
i have added custom column user profile table called coid (int). when want create new user want send value 1 coid, experiencing trouble , cant work properly.
i using standard account tables mvc4.
here of code
accountmodel.cs
public class registermodel { [required] [display(name = "user name")] public string username { get; set; } [required] [stringlength(100, errormessage = "the {0} must @ least {2} characters long.", minimumlength = 6)] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } [datatype(datatype.password)] [display(name = "confirm password")] [compare("password", errormessage = "the password , confirmation password not match.")] public string confirmpassword { get; set; } [datatype(datatype.custom)] [display(name = "coid")] [compare("coid", errormessage = "plese insert coid")] public int coid { get; set; } } [table("userprofile")] public class userprofile { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int userid { get; set; } public string username { get; set; } public int coid { get; set; } }
accountcontroller.cs
[httppost] [allowanonymous] [validateantiforgerytoken] public actionresult register(registermodel model) { model.coid = 1; if (modelstate.isvalid) { // attempt register user try { websecurity.createuserandaccount(model.username, model.password, model.coid); websecurity.login(model.username, model.password); return redirecttoaction("index", "home"); } catch (membershipcreateuserexception e) { modelstate.addmodelerror("", errorcodetostring(e.statuscode)); } } // if got far, failed, redisplay form return view(model); }
when executing code getting error:
the custom data-type string cannot null or empty.
i think call websecurity.createuserandaccount might need looking at.
perhaps following might work better:
websecurity.createuserandaccount(model.username, model.password, new { coid = model.coid });
as anonymous object can work out values you're trying assign field names. otherwise you're passing through value , doesn't know put it.
Comments
Post a Comment