【问题标题】:From a view can I call a method in the controller? [duplicate]从视图中我可以调用控制器中的方法吗? [复制]
【发布时间】:2014-01-06 23:10:23
【问题描述】:

在视图中如何调用控制器中的方法或项目中的另一个类?我可以这样做吗?

【问题讨论】:

    标签: asp.net asp.net-mvc-4


    【解决方案1】:

    如果您需要在视图中调用操作方法,则需要使用ChildActionOnly

    [ChildActionOnly]
    public ActionResult action1()
    {
       //
       return PartialView();
    }
    

    那么在你看来:

    @Html.Partial("action1")
    

    但是如果你想调用一个像帮助类这样的方法,你可以这样:

    @helper ShowTree()
    {
       //some code
    }
    

    【讨论】:

      【解决方案2】:

      试试这个,

      下面提到如何查看调用控制器动作事件的所有格式。

      -> 使用 Ajax beginform 调用

       @using (Ajax.BeginForm("ContainerSizeDetail", "Content", FormMethod.Post, null, new { @class = "" }))
              {}
      [HttpPost]
              public ActionResult ContainerSizeDetail(ContainerSizeModel model)
              {}
      

      -> 使用 Beginform 调用

              @using (Html.BeginForm("VendorContactDetail", "VendorAccount", FormMethod.Post, new { id = "frmVendorContact" }))
                      {}
      [AllowAnonymous]
              public ActionResult VendorContactDetail()
              {}
      

      -> 使用 Ajax 调用

              var request = $.ajax({
                              url: "http://localhost/ProjectDirectory/VendorAccount/ValidateUser",
                              type: 'POST',
                              cache: false,
                              data: JSON.stringify(returnValue),
                              dataType: 'json',
                              contentType: 'application/json; charset=utf-8'
                          });
      
      [AllowAnonymous]
              [HttpPost]
              public ActionResult ValidateUser(VendorRegistrationModel model)
              {}
      

      【讨论】:

        猜你喜欢
        • 2013-08-26
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多