单个MVC页面上的多个表单

时间:2014-07-28 13:26:54

标签: c# asp.net-mvc

以下是我的页面的屏幕截图。它有3个选项卡 - 详细信息,用户和权限。每个选项卡都有自己的提交按钮,我需要在控制器中触发不同的代码(目前还没有代码)。

Screenshot of application

我的问题是,如何在一个MVC页面上有3个单独的表单,如何让每个Submit按钮运行相应的代码?

查看

<div class="row-fluid">
<div class="span12">
    <!-- BEGIN INLINE TABS PORTLET-->
    <div class="widget">
        <div class="widget-title">
            <h4><i class="icon-user"></i></h4>
        </div>
        <div class="widget-body">
            <div class="row-fluid">
                <div class="span8">
                    <!--BEGIN TABS-->
                    <div class="tabbable custom-tab">
                        <ul class="nav nav-tabs">
                            <li class="active"><a href="#tab_1_1" data-toggle="tab">Role Details</a></li>
                            <li><a href="#tab_1_2" data-toggle="tab">Users</a></li>
                            <li><a href="#tab_1_3" data-toggle="tab">Rights</a></li>
                        </ul>
                        <div class="tab-content">
                            <div class="tab-pane active" id="tab_1_1">
                                @using (Html.BeginForm("Details", "RoleController", FormMethod.Get, new { }))
                                {
                                    @Html.ValidationSummary(true)    
                                    <div class="row-fluid">
                                        <div class="span3">
                                            @Html.HiddenFor(model => model.Id)
                                            <div class="control-group">
                                                <label class="control-label">@Html.DisplayNameFor(model => model.RoleName)</label>
                                                <div class="controls controls-row">
                                                    @Html.EditorFor(model => model.RoleName)
                                                    @Html.ValidationMessageFor(model => model.RoleName)
                                                </div>
                                            </div>
                                            <div class="control-group">
                                                <label class="control-label">@Html.DisplayNameFor(model => model.Description)</label>
                                                <div class="controls controls-row">
                                                    @Html.TextArea("Description", Model.Description, new { @cols = 400, @rows = 10 })
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <input type="submit" class="btn btn-success" value="Save" />  
                                }
                            </div>

                            <div class="tab-pane" id="tab_1_2">
                                @using (Html.BeginForm("UsersForRole", "RoleController", FormMethod.Post, new { }))
                                {                                    
                                    <!-- BEGIN DUAL SELECT-->
                                    <div>
                                        <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTk5MjI0ODUwOWRkJySmk0TGHOhSY+d9BU9NHeCKW6o=" />
                                    </div>
                                    <div>
                                        <table style="width: 100%;" class="">
                                            <tr>
                                                <td style="width: 35%">
                                                    <div class="d-sel-filter">
                                                        <span>Filter:</span>
                                                        <input type="text" id="box1Filter" />
                                                        <button type="button" class="btn" id="box1Clear">X</button>
                                                    </div>

                                                    <select id="box1View" multiple="multiple" style="height: 300px; width: 75%" runat="server">
                                                    </select><br />

                                                    <span id="box1Counter" class="countLabel"></span>

                                                    <select id="box1Storage">
                                                    </select>
                                                </td>
                                                <td style="width: 21%; vertical-align: middle">
                                                    <button id="to2" class="btn" type="button">&nbsp;>&nbsp;</button>

                                                    <button id="allTo2" class="btn" type="button">&nbsp;>>&nbsp;</button>

                                                    <button id="allTo1" class="btn" type="button">&nbsp;<<&nbsp;</button>

                                                    <button id="to1" class="btn" type="button">&nbsp;<&nbsp;</button>
                                                </td>
                                                <td style="width: 35%">
                                                    <div class="d-sel-filter">
                                                        <span>Filter:</span>
                                                        <input type="text" id="box2Filter" />
                                                        <button type="button" class="btn" id="box2Clear">X</button>
                                                    </div>

                                                    <select id="box2View" multiple="multiple" style="height: 300px; width: 75%;">
                                                        @foreach (var user in Model.Users)
                                                        {
                                                            <option>@Html.DisplayFor(model => user.SurnameFirstName)</option>
                                                        }
                                                    </select><br />

                                                    <span id="box2Counter" class="countLabel"></span>

                                                    <select id="box2Storage">
                                                    </select>
                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                    <div class="mtop20">
                                        <input type="submit" value="Submit" class="btn" />
                                    </div>
                                    <!-- END DUAL SELECT-->
                                }
                            </div>

                            <div class="tab-pane" id="tab_1_3">
                                @using (Html.BeginForm("RightsForRole", "RoleController", FormMethod.Post, new { }))
                                {                                    
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Right Name</th>
                                                <th>Description</th>
                                                <th></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach (var right in Model.Rights)
                                            {
                                                <tr>
                                                    <td>@Html.DisplayFor(model => right.RightName)</td>
                                                    <td>@Html.DisplayFor(model => right.Description)</td>
                                                    <td>
                                                        <div class="success-toggle-button">
                                                            @Html.CheckBoxFor(model => right.Assigned, new { @class = "toggle" })
                                                        </div>
                                                    </td>
                                                </tr>
                                            }
                                        </tbody>
                                    </table>
                                }
                            </div>
                        </div>
                    </div>
                    <!--END TABS-->
                </div>
                <div class="span4">
                    <div class="control-group">
                        <label class="control-label">@Html.DisplayNameFor(model => model.DateCreated)</label>
                        <div class="controls controls-row">
                            @Html.EditorFor(model => model.DateCreated, new { @readonly = "readonly" })
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">@Html.DisplayNameFor(model => model.CreatedBy)</label>
                        <div class="controls controls-row">
                            @Html.EditorFor(model => model.CreatedBy, new { @readonly = "readonly" })
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">@Html.DisplayNameFor(model => model.LastUpdated)</label>
                        <div class="controls controls-row">
                            @Html.EditorFor(model => model.LastUpdated)
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">@Html.DisplayNameFor(model => model.LastUpdateBy)</label>
                        <div class="controls controls-row">
                            @Html.EditorFor(model => model.LastUpdateBy)
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

UsersForRole控制器

[HttpPost]
    public ActionResult UsersForRole(RoleModel model)
    {
        if (ModelState.IsValid)
        {

        }
        return View(model);
    }

4 个答案:

答案 0 :(得分:4)

你在做什么看起来很好,你只需要在控制器中添加其他动作......

详细

RightsForRole

答案 1 :(得分:4)

我认为你在这一行可能有问题:

 @using (Html.BeginForm("UsersForRole", "RoleController", FormMethod.Post, new { }))

Html.BeginForm shoudl中的控制器名称只有Role,除非您的控制器名称是`MoreControllerController?

答案 2 :(得分:0)

我认为你应该使用Partial Views

答案 3 :(得分:0)

您只需使用三个不同的页面(操作)即可在一个表单中实现多个表单,并使用tab作为Actionlink。在Actionlink上,您可以调用相应的操作。

如果您发布表单,将会出现问题。

否则你应该使用JQuery来实现这种情况。