c# - Pass values from view to controllers -
i starting off mvc framework , trying things. 1 of them create simple registration page has few textboxes , passes values controller in turn saves database.
i have used form posting method achieve similar task
@using (html.beginform()) { @html.label("username:") @html.textbox("texbotusername", "") <br /> @html.label("password:") @html.textbox("texbotpassword", "") <br /> <input type="submit" value="signin" name="action:signin" /> <input type="submit" value="register" name="action:register" /> } [httppost] [multibutton(name = "action", argument = "signin")] public actionresult signin(string textboxusername) { return content(textboxusername); }
apart above, there better way pass values (using models maybe?) per best practices stuff.
thanks answers.
designing model :
public class userloginmodel { [required(errormessage = "please enter username")] [display(name = "user name")] [maxlength(50)] public string username { get; set; } [required(errormessage = "please enter password")] [display(name = "password")] [maxlength(50)] public string password { get; set; } }
presenting model in view :
@model mymodels.userloginmodel @{ viewbag.title = "user login"; layout = "~/views/shared/_layout.cshtml"; } @using (html.beginform()) { <div class="editor-label"> @html.labelfor(m => m.username) </div> <div class="editor-field"> @html.textboxfor(m => m.username) @html.validationmessagefor(m => m.username) </div> <div class="editor-label"> @html.labelfor(m => m.password) </div> <div class="editor-field"> @html.passwordfor(m => m.password) @html.validationmessagefor(m => m.password) </div> <p> <input type="submit" value="log in" /> </p> </div> }
working action
[httppost] public actionresult login(userloginmodel user) { // implementation return view(user); }
Comments
Post a Comment