简体   繁体   English

如何在视图首次加载时进行局部视图加载?

[英]How to make a partial view load when the view first loads?

I finally learned how to use Ajax.BeginForm to make a table refresh when submitting select choices in the BeginForm block. 我终于学会了如何使用Ajax.BeginForm在BeginForm块中提交选择选项时刷新表。

When I click "submit" the partial view loads as intended, every time, so that functionality is dialed in. 当我单击“提交”时,每次都会按预期加载部分视图,以便拨打功能。

What I have not been able to find is how I tell the Controller to load the Partial View when the View first loads, using the default values in the Index Action. 我无法找到的是如何使用Index Action中的默认值告诉Controller在View首次加载时加载Partial View。 (The Partial View supports the Index Action). (部分视图支持索引操作)。

Is this something I can do from the Controller or does it need to be jQuery? 这是我可以从Controller做的事情还是需要jQuery?

To provide more context, the following code is what currently works -- that is, the partial view loads only when the form is submitted, but doesn't show on first View load. 为了提供更多上下文,以下代码是当前工作的 - 也就是说,部分视图仅在提交表单时加载,但不会在第一个View加载时显示。

View: 视图:

@model ViewModelTest.ViewModels.StudentBehaviorDetailBySemesterViewModel

// ... code ...

@using (Ajax.BeginForm(actionName: "PartialTable",
    controllerName: "StudentBehaviorDetailBySemester",
    ajaxOptions: new AjaxOptions { UpdateTargetId = "TableData" }))
{
    <div>
        <p>Campus: @Html.DropDownListFor(c => c.SelectedCampus, Model.CampusList)</p>
        <p>FY: @Html.DropDownListFor(c => c.SelectedFy, Model.FyList)</p>
        <input type="submit" value="submit" />
    </div>
}

<div id="TableData">
    @{Html.Action("PartialTable", "StudentBehaviorDetailBySemester"
          , new { vm = Model });}
</div>


@section scripts
{
    <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
}

Controller -- Index and Partial View 控制器 - 索引和部分视图

[HttpGet]
public ActionResult Index(string SelectedCampus="MRA", string SelectedFy="FY16")
{
    StudentBehaviorDetailBySemesterViewModel vm 
      = new StudentBehaviorDetailBySemesterViewModel();    
    vm.SelectedCampus = SelectedCampus;
    vm.SelectedFy = SelectedFy;

    vm.CampusList = new SelectList(Repositories.SelectLists.CampusList());
    vm.FyList = new SelectList(Repositories.SelectLists.FiscalYearList());
    return View(vm);
}


public ActionResult PartialTable(StudentBehaviorDetailBySemesterViewModel vm)
{
    using (TestAccountContext db = new TestAccountContext())
    {
        var query = db.StudentBehaviorDetailBySemesters
            .Where(m => m.Campus == vm.SelectedCampus)
            .Where(m => m.Fy == vm.SelectedFy).ToList();
        vm.BehaviorDetail = query;
    }

    return PartialView("PartialTable", vm);
}

如果要渲染局部视图,则更新部分视图语法...

@{Html.RenderAction("PartialTable", "ControllerName", new { vm = Model });}

Instead of 代替

@{Html.Action("PartialTable", "StudentBehaviorDetailBySemester", new { vm = Model });}

You need to use: 你需要使用:

@{Html.RenderAction("PartialTable", "StudentBehaviorDetailBySemester", new { vm = Model });}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM