WebGrid列中的MVC3 Html.BeginForm?

sup*_*ime 5 webgrid asp.net-mvc-3

我今晚早些时候有一个疯狂的想法,并且完成了3/4的方法并且遇到了一个奇怪的问题.我想在控制器上自动生成所有方法的索引,而不是返回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; }
}
Run Code Online (Sandbox Code Playgroud)

反映当前控制器上所有操作的操作:

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);
}
Run Code Online (Sandbox Code Playgroud)

在视图中,我只想使用一个简单的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>
Run Code Online (Sandbox Code Playgroud)

网格似乎正确呈现,但奇怪的是,所有表单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="$(&#39;#grid&#39;).load(&#39;/Test/SecretIndex?sort=ActionName&amp;sortdir=DESC&amp;__=634581349851993336 #grid&#39;);">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>
Run Code Online (Sandbox Code Playgroud)

如您所见,标签在表格之外呈现!知道我在这里做错了吗?或者你可以不在Webgrid中 BeginForm吗?制作一堆个人表格的更好方法是什么?

提前致谢!

Jak*_*cki 3

<form>尝试在不使用助手的情况下自行渲染。

看起来 lambda 表达式是在 helper 吐出内容之前在 helper 内部执行的,这会导致BeginForm立即渲染输出。