我有一个主视图和 2 个局部 View 。
1 部分 View 有:
- 下拉列表
- 文本框
- 添加按钮
第二部分 View 有:
- 两个文本框
- 其中一个显示第一个partialView的DropDownList的值
- 然后显示 TextBox 的第一个分部 View 的值。
我试图实现的功能是,当 Add Button
上发生 Click Event
时,DropDownList
和 TextBox 中的数据
应该出现在第二个局部 View TextBoxes
中(此外,TextBoxes
的数量取决于 的
已收到)。我目前对如何传递和保存数据感到困惑。Clicks
次数添加按钮
下面是第一个部分 View 的代码(包括 DropDownList
和 TextBox
)。
<% using (Html.BeginForm("AddServices", "Controller1", FormMethod.Post, new { id = "form" }))
{%>
<%: Html.ValidationSummary(true) %>
<div class="edit-set">
<label>Nature of Service:</label>
<div class="editor-field">
<%: Html.DropDownList("ddlServiceType", (SelectList)ViewData["ServiceType"] as SelectList, "--Select Nature Of Service--", new { onchange = "FillServiceType();" })%>
<%: Html.HiddenFor(model => model.Id)%>
<label>Comment:</label>
<input type="text" id="comment" />
</div>
<input type="submit" value="+ Add More Service(s)" onclick="InsertService()" name="submitButton"/>
</div><input type="submit" value="Done" name="submitButton"/>
<% } %>
用户插入所需的服务后,第二部分 View 中的所有文本框值都必须插入到数据库中。我的问题是我必须维护所有值但不将数据插入数据库。它将在使用单击“完成”按钮后插入。(我也可以在第二个部分 View 中使用表,但该表的值必须可用于插入到数据库中。)
最佳答案
1 尝试渲染操作而不是渲染局部 View 。
如果 DropDown 和 Textbox 的值必须先保存在数据库中,这意味着您必须先提交数据。
因此该操作将从数据库中获取所需的数据以呈现第二个局部 View 并返回它。
查看渲染操作:http://haacked.com/archive/2009/11/17/aspnetmvc2-render-action.aspx
<% using (Html.BeginForm("AddServices", "Controller1", FormMethod.Post, new { id = "form" }))
{%>
<%: Html.ValidationSummary(true) %>
<div class="edit-set">
<label>Nature of Service:</label>
<div class="editor-field">
<%: Html.DropDownList("ddlServiceType", (SelectList)ViewData["ServiceType"] as SelectList, "--Select Nature Of Service--", new { onchange = "FillServiceType();" })%>
<%: Html.HiddenFor(model => model.Id)%>
<label>Comment:</label>
<input type="text" id="comment" />
</div>
<input type="submit" value="+ Add More Service(s)" onclick="InsertService()" name="submitButton"/>
</div><input type="submit" value="Done" name="submitButton"/>
<%= Html.Action("MyAction", "Controller", new {ID = Model.id}) %>
<% } %>
关于c# - 将一个局部 View 数据传递给另一个局部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15204037/