【发布时间】:2014-07-14 01:18:08
【问题描述】:
我正在使用 mvc4 并正在使用 Html.BeginForm 在我的视图中调用另一个控制器
它工作正常!但这里使用文本框来传递值。 如何修改此代码,所以我正在使用
@Html.DisplayFor(modelItem => item.UserName) ....代替 @Html.TextBox("用户名")
这是我的看法:
图片:
@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>
这里是我的控制器:
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");
}
另一种观点:
它的图像:
@{
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>
}
【问题讨论】:
-
为什么要使用@Html.DisplayFor?为什么不@Html.EditorFor?
-
我只想显示值..没有输入字段和提交按钮..
-
我不清楚你想归档什么。您想要调用另一个控制器的表单,但您不想要提交按钮,但是您打算如何将数据发布到控制器?或者带有@Html.DisplayFor(modelItem => item.UserName) 的代码不起作用,您想知道如何正确使用它?
-
没有代码工作 再次看到 Q 我更新它。我使用提交按钮转到另一个视图 ..我不希望我想在表格和相同的视图中显示它们,我不知道怎么样!
-
看来我了解您要归档的内容。基本上你想摆脱这个用户按钮的获取角色,只让第三列工作。当您使用 _db.UserProfiles.ToList() 进行查询时,重要的是要知道您在 UserProfile 中是否具有当前用户的一个或多个角色?
标签: c# html asp.net-mvc-4