MVC - 当返回视图不同时,是否可以从另一个ActionResult函数调用actionResult函数

时间:2017-05-17 00:30:23

标签: ajax asp.net-mvc

我有一个 SuperMapModel

public class SuperMapModel
{
    public StudentAssessmentViewModel saVM { get; set; }
    public SearchControlViewModel scM { get; set; }
}

在我的页面上关于.cshtml标记是

@model SuperMapModel

<div>
@Html.Partial("_SearchPanel", Model.scM)
</div>
<div class="row">
<div class="col-md-12 table-responsive" id="mapsDiv">
    @Html.Partial("~/Views/Maps/_MapDetailsList.cshtml", Model.saVM)

</div>

从我的 HomeController 我设置上述属性并返回模型如下

 public ActionResult About()
 { 
       SuperMapModel smM = new SuperMapModel();
       smM.scM = new SearchControlViewModel();
        smM.scM.x = ..
        smM.scM.y = ..

        smM.saVM = new StudentAssessmentViewModel();
        smM.saVM.x = ..
        smM.saVM..y = ..
        smM.saVM..z = ..

        return View(smM);
 }

现在重新加载页面时,我只通过ajax刷新页面中的局部视图_MapDetailsList.cshtml。所以我在MapsController中调用另一个函数(来自javascript),它与上面几乎完全相同

public ActionResult DisplaySearchResults(int mapHeaderId, int yearId, string classIds, int indicatorGroup, string indicatorIds)
{
       SuperMapModel smM = new SuperMapModel();
        smM.saVM = new StudentAssessmentViewModel();

       smM.saVM.x = ..
        smM.saVM..y = ..
        smM.saVM..z = ..
        return PartialView("~/Views/Maps/_MapDetailsList.cshtml", smM.saVM);
}

我可以从About()中调用DisplaySearchResults进行重构吗?

1 个答案:

答案 0 :(得分:1)

编写第三种方法并从其他方法中调用它。

class AddStartDateToUser < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :start_date, :datetime
  end
end