今晚早些时候我有一个有点疯狂的想法,实现了 3/4,却遇到了一个奇怪的问题。我想自动生成 Controller 上所有方法的索引,而不是返回 ActionResult ,以及每个人提交有效数据的简单表单。通过反射似乎是一件非常简单的事情:
Quickie ViewModel 来保存每个反射操作:
public class ReflectedAction
{
public ReflectedAction(MethodInfo methodInfo, string controllerName)
{
this.ActionName = methodInfo.Name;
this.ControllerName = controllerName;
this.Parameters = methodInfo.GetParameters().Select(p => p.Name);
}
public string ControllerName { get; set; }
public string ActionName { get; set; }
public IEnumerable<string> Parameters { get; set; }
}
反射(reflect)当前 Controller 上所有操作的操作:
public virtual ActionResult AutoIndex()
{
Type controllerType = this.ControllerContext.Controller.GetType();
string controllerName = controllerType.Name.Replace("Controller", string.Empty);
var methods = this.ControllerContext.Controller.GetType().GetMethods().Where(
m => m.ReturnType.Name.Contains("ActionResult"));
var model = methods.Select(m => new ReflectedAction(m, controllerName));
return View(model);
}
在 View 中,我只想使用一个简单的 WebGrid 将每个操作呈现为一行,第一列是操作的名称,第二列是一个迷你表单,能够填充出该操作具有的任何字段(我尝试将其作为辅助工具,或以网格格式内联,后者包含在此处:
@using TfsMvc.Controllers
@model IEnumerable<TestController.ReflectedAction>
@{
ViewBag.Title = "AutoIndex";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>AutoIndex</h2>
@{
var grid = new WebGrid(
source: Model,
ajaxUpdateContainerId: "grid",
defaultSort: "ActionName",
canPage: false);
}
<div id="grid">
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("ActionName"),
grid.Column(format: (action) =>
{
using (Html.BeginForm((string)action.ActionName, (string)action.ControllerName, FormMethod.Get))
{
string htmlString = string.Empty;
foreach (string parameter in action.Parameters)
{
htmlString = "<span>" + Html.Label(parameter) + Html.TextBox(parameter) + "</span>";
}
htmlString += "<input type=\"submit\" />";
return new HtmlString(htmlString);
}
}))
)
</div>
网格似乎正确呈现,但奇怪的是所有表单 html 标签都呈现在网格之外,但控件呈现在网格内:
<div id="grid">
<form action="/Test/CloneTestPlan" method="get"></form>
<form action="/Test/ConfigureTestPlan" method="get"></form>
<form action="/Test/EnvConfig" method="get"></form>
<form action="/Test/FixTestLink" method="get"></form>
<!-- ton of other actions snipped-->
<table class="grid">
<thead>
<tr class="head"><th scope="col"><a href="#" onclick="$('#grid').load('/Test/SecretIndex?sort=ActionName&sortdir=DESC&__=634581349851993336 #grid');">ActionName</a></th><th scope="col"></th></tr>
</thead>
<tbody>
<tr><td>CloneTestPlan</td><td><span><label for="subid">subid</label><input id="subid" name="subid" type="text" value="" /></span><input type="submit" /></td></tr>
<tr class="alt"><td>ConfigureTestPlan</td><td><span><label for="apply">apply</label><input id="apply" name="apply" type="text" value="" /></span><input type="submit" /></td></tr>
<tr><td>EnvConfig</td><td><span><label for="create">create</label><input id="create" name="create" type="text" value="" /></span><input type="submit" /></td></tr>
<tr class="alt"><td>FixTestLink</td><td><span><label for="commit">commit</label><input id="commit" name="commit" type="text" value="" /></span><input type="submit" /></td></tr>
<!-- ton of other actions snipped-->
</tbody></table>
</div>
如您所见,标签呈现在表格的外部!知道我在这里做错了什么吗?或者您可以不在 Webgrid 中执行 BeginForm 吗?有没有更好的方法来制作一堆单独的表格?
提前致谢!
最佳答案
尝试渲染<form>
你自己不使用助手。
看起来 lambda 表达式是在 helper 吐出内容之前在 helper 内部执行的,这会导致 BeginForm
立即渲染输出。
关于asp.net-mvc-3 - WebGrid 列中的 MVC3 Html.BeginForm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8310325/