Am using mvc4 and am calling another controller in my view using Html.BeginForm
It work fine!but here am using textbox to pass the value. How to modify this code so am using
@Html.DisplayFor(modelItem => item.UserName) ....instead of @Html.TextBox("UserName")
here my view :
image of it:
@using OTMS.Models
@model IEnumerable<OTMS.Models.UserProfile>
@{
ViewBag.Title = "Index";
}
<!-- Table Continer -->
<div class="spacer_10px"></div>
<div class="container clearfix">
<div class="grid_12">
<div class="table_wrapper table_gray">
<table>
<tr>
<th>
<p>User Name</p>
</th>
<th>
<p>Role</p>
</th>
<th>
<p>Role</p>
</th>
</tr>
@if (Model != null) {
foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@using(Html.BeginForm("GetRoles", "Account",FormMethod.Post)){
@Html.AntiForgeryToken()
<div class="editor-label">Username : </div>
@Html.TextBox("UserName") //here user will enter user name / I dont want user to enter that ,it should be done Automatically
<div class="spacer_20px"></div>
<div class="button button-orange"> <span class=" form_button clearfix">
<input type="submit" class="submit" name="submit" value="Get Roles for this User" />
</span> </div>//by clicking that will pass the user name to controller (GerRole)/I dont want button
}
</td>
<td>
@using (Html.BeginForm("Submit", "Account", FormMethod.Post))
{
@Html.Hidden("userName", item.UserName)
@Html.DropDownList("selectedRole", (SelectList)ViewBag.Roles)
<div class="button button-orange"> <span class=" form_button clearfix">
<input type="submit" class="submit" name="submit" value="Update Change" />
</span> </div>
}
</td>
</tr>
}
}
</table>
</div> </div>
here my controller :
public ActionResult Index()
{
var model = _db.UserProfiles.ToList();
ViewBag.Roles = new SelectList(Roles.GetAllRoles());
return View(model);
}
[HttpPost]
public ActionResult GetRoles(string UserName)
{
if (!string.IsNullOrWhiteSpace(UserName))
{
ViewBag.RolesForThisUser = Roles.GetRolesForUser(UserName);
SelectList list = new SelectList(Roles.GetAllRoles());
ViewBag.Roles = list;
}
return View("showrole");
}
another view:
image of it :

@{
ViewBag.Title = "showrole";
}
<h2>showrole</h2>
@if(ViewBag.RolesForThisUser != null) {
<text>
<h3>Roles for this user </h3>
<ol>
@foreach (string s in ViewBag.RolesForThisUser){
<li>@s</li>
}
</ol>
</text>
}