从第一个视图获取值后,将视图放在其他视图中

时间:2015-07-08 14:17:00

标签: jquery ajax asp.net-mvc asp.net-mvc-5

我是MVC的新手。我有一个名为Index的动作,它填充一个选择列表并将其传递给视图。然后我有一个httpost函数GetBaseline()作为RedirectToRouteResult从Index.vbhtml获取值并将其传递给StatusList()以显示表。

这是我的StatusAreaController:

Function Index() As ActionResult            
        Dim l_SubmitBaselineSelectLists As New FCSWebMVC.Models.SubmitBaselineSelectLists
        With l_SubmitBaselineSelectLists
            .m_SelectListBaseline = m_BaselineRepository.BaselineSelectList()  'Pass the baselines obtained from the model to the view
        End With
        Return View(l_SubmitBaselineSelectLists)
    End Function


'This handles the POST resulting from the Submit button click
    <HttpPost()> _
    Public Function GetBaseline() As RedirectToRouteResult
        Dim l_iBaseline As Integer = Request.Form("cmbBaselineType")
        System.Web.HttpContext.Current.Cache("baselineId") = l_iBaseline
        Dim l_sBaselineName As String = m_BaselineRepository.GetBaselineName(l_iBaseline)
        System.Web.HttpContext.Current.Cache("baselineName") = l_sBaselineName
        Return RedirectToAction("StatusList", New With {.Bid = l_iBaseline})
    End Function

'action to display current features based on baseline
    Public Function StatusList(ByVal search As String, ByVal Bid As Integer) As ActionResult
        Return View(m_StatusRepository.GetStatusList(Bid, search))
    End Function

Index.vbhtml:

@ModelType FCSWebMVC.Models.SubmitBaselineSelectLists
@Code
ViewData("Title") = "Status Area"
End Code
<h4>@ViewData("Title")</h4>
<br />
<div>
@Using (Html.BeginForm("GetBaseline", "StatusArea"))
    @Html.Label("Select Baseline:")
    @Html.DropDownList("cmbBaselineType", Model.m_SelectListBaseline)@<br />@<br />

    @<input type="submit" value="Select!" />

End Using

StatusList.vbhtml:

@ModelType IEnumerable(Of FCSWebMVC.Status)

<link href="@Url.Content("~/Content/Tables.css")"
  rel="stylesheet" type="text/css" />

<br />
<div>

@Using (Html.BeginForm("GetSearchParameters", "StatusArea"))

    @<b>Search for Satus ID:</b>@<br />
    @Html.TextBox("search", String.Empty, New With {.size = "200"}) @<input type="submit" value="Find Status ID" />

End Using
</div>
<br />
<h4>Current Statuses:</h4>
<br />
@If (Model.Count > 0) Then
@<div class="table_div_status">
@Using (Html.BeginForm("StatusList", "StatusArea"))

    Dim grid = New WebGrid(canPage:=False, source:=Model)
    @grid.GetHtml(tableStyle:="webgrid-table", headerStyle:="webgrid-header", rowStyle:="webgrid-row-style", htmlAttributes:=New With {.id = "table1"}, columns:=grid.Columns(
        grid.Column("Status ID", format:=Function(str) Html.ActionLink(CType(str, System.Web.Helpers.WebGridRow).Value.Status_ID,
                                                                    "StatusData",
                                                                    New With {.BaselineID = CType(str, System.Web.Helpers.WebGridRow).Value.BaselineID,
                                                                              .StatusID = CType(str, System.Web.Helpers.WebGridRow).Value.Status_ID})),
        grid.Column("Level", format:=Function(str) Html.ActionLink(CType(str, System.Web.Helpers.WebGridRow).Value.Level,
                                                                    "StatusData",
                                                                    New With {.BaselineID = CType(str, System.Web.Helpers.WebGridRow).Value.BaselineID,
                                                                              .StatusID = CType(str, System.Web.Helpers.WebGridRow).Value.Status_ID})),
        grid.Column(columnName:="Approved_Short_String", header:="Approved Short String"),
        grid.Column(columnName:="Long_String", header:="Long String")))

End Using
</div>
Else
@<center><h3>No data found</h3></center>
End If

一旦用户点击提交按钮,我希望能够在与选择列表正下方的索引视图相同的页面中显示StatusList视图。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

假设您知道如何使用基本JavaScript,则需要使用AJAX执行此操作。我有一个答案here,它解决了与您的问题相似的问题。如果您打算使用jQuery,我会回答使用jQuery执行AJAX请求的方式稍微更新一下,jQuery.get,否则您需要使用vanilla JS查看XHR请求。

您要特别做的是通过AJAX将您的信息发送到控制器并返回PartialResult,或者只返回PartialView

答案 1 :(得分:0)

You could use partial views.

  1. Create your second view as a partial view
  2. Make an AJAX call from the current view to an action method in a controller
  3. Return the partial view from the action method
  4. Append the result to a div

Hope the below link helps http://www.c-sharpcorner.com/UploadFile/d551d3/how-to-load-partial-views-in-Asp-Net-mvc-using-jquery-ajax/