Tuesday 15 February 2011

c# - RadioButtonFor saving Null in DB [ASP.NET MVC 4] -



c# - RadioButtonFor saving Null in DB [ASP.NET MVC 4] -

i'm using asp.net mvc 4 template create application.

i created usertype string in userprofile field , migration update database. it's right.

my problem is, when i'll register new user in register view, application save fields, usertype, saves null, how can resolve it? read topics similar questions, don't know how work me. code below:

with help of @stephenmuecke ,i'm here posting code updated, problem not fixed yet:

//models [table("userprofile")] public class userprofile { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int userid { get; set; } public string username { get; set; } public string usertype { get; set; } //flag de tipo usuário musico ou ouvinte } public class registermodel { [required] [display(name = "user name")] public string username { get; set; } [required] [stringlength(100, errormessage = "the {0} must @ to the lowest degree {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; } [required] [display(name = "tipo de usuario")] public string usertype { get; set; } } //controller // get: /account/register [allowanonymous] public actionresult register() { homecoming view(); } // // post: /account/register [httppost] [allowanonymous] [validateantiforgerytoken] public actionresult register(registermodel model) { if (modelstate.isvalid) { // effort register user seek { var user = new userprofile() { username = model.username, usertype = model.usertype }; websecurity.createuserandaccount(model.username, model.password); websecurity.login(model.username, model.password); homecoming redirecttoaction("index", "home"); } grab (membershipcreateuserexception e) { modelstate.addmodelerror("", errorcodetostring(e.statuscode)); } } //view @model testtcc2.models.registermodel <div class="form-group"> <label for="icode" class="col-md-3 control-label">tipo de usuário</label> <div class="col-md-9"> @*html.dropdownlistfor(m=> m.usertype, usertypes)*@ @html.radiobuttonfor(m => m.usertype, "ouvinte", new { id = "ouvinte" }) @html.label("ouvinte") @html.radiobuttonfor(m=>m.usertype, "musico", new { id= "musico" }) @html.label("musico") </div> </div>

edit:

to prepare problem radiobutton, need update post register method calling userprofile class.

this code: http://pastebin.com/mwd4qd6u

and i'm create new topic inquire exception error i'm getting during register topic link: membershipexception during user register @ asp.net

try

@html.radiobuttonfor(m => m.usertype, "ouvinte", new { id = "ouvinte" }) @html.label("ouvinte") @html.radiobuttonfor(m=>m.usertype, "musico", new { id= "musico" }) @html.label("musico")

c# asp.net-mvc asp.net-mvc-4 radio-button html.dropdownlistfor

No comments:

Post a Comment