MVC:表单不在局部视图中呈现

时间:2010-11-22 09:13:13

标签: asp.net-mvc

我有一个带标签的表单。从属于每个选项卡的是局部视图。 当用户提交保存更改时,我想为该局部视图调用一个方法,因此对于我放入的部分视图<%using(Html.BeginForm(...)%>。但是这会被忽略并单击提交将调用与容器视图关联的方法。为什么这样,以及如何使其工作?

视图看起来像;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Employee.Master" Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.HolidayRequestViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    HolidayRequest
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="EmployeeContent" runat="server">
    <% using (Html.BeginForm()) {%>
    <%: Html.AntiForgeryToken() %>
    <h2>Holiday Request</h2>
    <p>You have <%: Html.DisplayFor(model => model.DaysAvailableThisYear) %> days left annual leave for this year 
    and <%: Html.DisplayFor(model => model.DaysAvailableNextYear) %> days left for next year.</p>
    <p>If your request is approved and exceeds the number of days left, then those extra days will not be paid.</p>
    <div id="tabs">
        <ul>
            <li><a href="#tabs-1">Approver</a></li>
            <li><a href="#tabs-2">Single Date</a></li>
            <li><a href="#tabs-3">Date Range</a></li>
         </ul>
         <div id="tabs-1">
            <% Html.RenderPartial("GetApproverTab", Model); %>
         </div>
         <div id="tabs-2">
            <% Html.RenderPartial("GetSingleDateTab", Model); %>
         </div>
         <div id="tabs-3">
            <% Html.RenderPartial("GetDateRangeTab", Model); %>
         </div>
    </div>
    <%: Html.HiddenFor(x => x.EmployeeId) %>
            <% } %>
</asp:Content>

部分视图看起来像;

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.WebUI.Models.HolidayRequestViewModel>" %>

<% using (Html.BeginForm("GetSingleDateTab", "Employee", FormMethod.Post, new { id = "frmSingleDate" }))
       { %>
<p>Enter your day or part day of Annual Leave here.</p>
<table>
    <tr>
        <td align="right">Date:</td>
        <td><%: Html.EditorFor(x => x.SingleDate) %></td>
    </tr>
    <tr>
        <td align="right">Morning Only:</td>
        <td><%: Html.CheckBoxFor(x => x.MorningOnlyFlag) %></td>
    </tr>
    <tr>
        <td align="right">Afternoon Only:</td>
        <td><%: Html.CheckBoxFor(x => x.MorningOnlyFlag) %></td>
    </tr>
    <tr>
        <td colspan="2" align="center"><input type="submit" value="Save" /></td>
    </tr>
</table>
    <% } %>

和控制器看起来像;

#region Employee Holiday Request

public ActionResult HolidayRequest()
{
    return View(new HolidayRequestViewModel(Employee.GetLoggedInUser().EmployeeId));
}

[HttpPost] // This is the method that gets executed
public ActionResult HolidayRequest(HolidayRequestViewModel hrvm)
{
    if (ModelState.IsValid)
    {
        hrvm.Update();
        return View(new HolidayRequestViewModel(hrvm.EmployeeId));
    }
    return View(hrvm);
}

[HttpPost] // This is the method I want to get executed.
public ActionResult GetSingleDateTab(HolidayRequestViewModel hrvm)
{
    if (ModelState.IsValid)
    {
        hrvm.Update();
        return View(new HolidayRequestViewModel(hrvm.EmployeeId));
    }
    return View("GetSingleDateTab", hrvm);
}

#endregion

2 个答案:

答案 0 :(得分:3)

你不能在html中拥有nester表单。

答案 1 :(得分:2)

我发现了一个令人难以置信的解决方案。只需插入一个&lt; script&gt; &lt; form&gt;上方的标记标签

<script type="text/javascript">
  // don't remove <script> tag
</script>

<form ....