c# - Passing parameters on button click in strongly-typed view to another controller -
i have strongly-typed view (bound usercontroller) lists user particular roles , below have dropdownlist containing roles submit button. need assign new role user. actionresult method in userrolescontroller. how can pass userid , roleid on button click actionresult method.
actionresult method in userrolescontroller:
[httppost] [validateantiforgerytoken] public actionresult addrole(userrole userrole, int roleid, int userid) { if (!modelstate.isvalid) return view(userrole); var check = db.userroles.any(x => x.roleid == roleid && x.userid == userid); if (check) viewbag.resultmessage = "this user has role specified !"; else db.userroles.add(userrole); db.savechanges(); viewbag.resultmessage = "user added role succesfully !"; return redirecttoaction("index"); }
view this:
@model ienumerable<mvcappcrud.user> @{ viewbag.title = "assignrole"; } <h2>assign role</h2> @if (!model.any()) { @html.label("no roles assigned user") } else { <table> <tr> <th> @html.displayname("email") </th> <th> @html.displayname("role name") </th> <th></th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.email) </td> <td> @html.displayfor(modelitem => item.rolename) </td> <td> @html.actionlink("delete", "delete", new {id = item.id}) </td> </tr> } </table> } <hr /> <div class="display-label"> @html.displayname("add role") </div> <div class="display-field"> @html.dropdownlist("roles", (selectlist) viewbag.roles) </div> @using (html.beginform("addrole", "userroles")) { <div class="message-success">@viewbag.resultmessage</div> } <p> <input type="submit" value="assign" /> </p> <p> @html.actionlink("back list", "index") </p>
model entities:
public partial class userrole { public int id { get; set; } public int userid { get; set; } public int roleid { get; set; } public int status { get; set; } public virtual user users { get; set; } public virtual role roles { get; set; } } public partial class user { public user() { roles = new list<selectlistitem>(); } public long id { get; set; } public string email { get; set; } public string password { get; set; } public system.datetime reg_date { get; set; } public byte validated { get; set; } public virtual icollection<userrole> userroles { get; set; } public int roleid { get; set; } public string rolename { get; set; } public ienumerable<selectlistitem> roles { get; set; } //public ienumerable<role> roles { get; set; } } public partial class role { public int id { get; set; } public string rolename { get; set; } public string desc { get; set; } public int status { get; set; } public virtual icollection<userrole> userroles { get; set; } }
on button click nothing happens. is possible pass values parameters 1 model view another?
there numerous problems code. in particular passing ienumerable<user>
model not including or rendering controls in form nothing posts back, , in case cant post userrole
because complex object , dropdownlist returns single value. , there no point displaying roles in dropdown, checking if been selected on postback - include roles user not have when create view. , assigning message viewbag
, redirecting pointless - lost.
create view model represent want display , edit (note have excluded properties displaying existing roles)
public class userrolevm { public int id { get; set; } // user id post public int name { get; set; } // user name display in view [display(name="select new role")] public int selectedrole { get; set; } public selectlist rolelist { get; set; } }
controller
public actionresult addrole(int id) { userrolevm model = new userrolevm(); var user = // user based on id model.id = id; model.name = user.?? var roles = // roles , remove user has model.rolelist = new selectlist(roles, "id", "rolename"); return view(model); }
view
@model userrolevm @using(html.beginform()) { <h2>@model.name</h2> // users name @html.labelfor(m => m.selectedrole) @html.dropdownlistfor(m => m.selectedrole, model.rolelist) <input type="submit" value="add role" /> }
post method
[httppost] public actionresult addrole(userrolevm model) { // model populated id of user , id of selected role // save , redirect }
Comments
Post a Comment