今晚我有一个有点疯狂的想法,已经实现了3/4并遇到了奇怪的问题.. 我想自动生成控制器上所有返回ActionResult的方法的索引,以及每个方法的简单表单来提交有效数据.. 通过反射似乎很容易做到:
快速的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; }
}
在当前控制器上反映所有操作的行动:
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);
}
在视图中,我只想使用一个简单的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?有更好的方法来创建多个单独的表单吗?
谢谢!