c# - 将一个局部 View 数据传递给另一个局部 View

标签 c# ajax asp.net-mvc-2

我有一个主视图和 2 个局部 View 。

1 部分 View 有:

  • 下拉列表
  • 文本框
  • 添加按钮

第二部分 View 有:

  • 两个文本框
  • 其中一个显示第一个partialView的DropDownList的值
  • 然后显示 TextBox 的第一个分部 View 的值。

我试图实现的功能是,当 Add Button 上发生 Click Event 时,DropDownListTextBox 中的数据 应该出现在第二个局部 View TextBoxes 中(此外,TextBoxes 的数量取决于 Clicks 次数添加按钮 已收到)。我目前对如何传递和保存数据感到困惑。

下面是第一个部分 View 的代码(包括 DropDownListTextBox)。

<% 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/

相关文章:

c# - 让线程等待退出而不求助于 Thread.Sleep()

jquery - 如何使用按钮加载更多网格对象

jquery - ASP.Net MVC2 中的级联下拉列表

javascript - 在 WordPress 主题中通过 $.ajax 调用外部 url

ajax - 下载需要身份验证 token 的文件

asp.net-mvc-2 - 混合 WebForms 和 MVC : What should I do with the MasterPage?

asp.net - MVC Contrib Grid 是 Asp.Net MVC 2.0 的一部分吗?

C# 委托(delegate)给结构方法

c# - 带参数的 OdbcCommand 不返回任何内容

c# - DryIoc - 在使用键名注册基类时解析派生类