【问题标题】:How to execute an action from a partial view using ASP.Net MVC?如何使用 ASP.Net MVC 从局部视图执行操作?
【发布时间】:2019-10-03 07:57:56
【问题描述】:

我在从另一个视图中使用的局部视图执行操作时遇到问题。

我试过这个用于更改 div 内容的 JQuery

$(document).ready(function () {
        //File Upload Button
        $(".fileUpload").click(function () {
            $.ajax({

                type: 'GET',
                url: '@Url.Action("FileUpload", "Dashboard")',
                data: { type: $(this).attr("data-type") },
                success: function (response) {
                    $(".content").html(response);
                },
                error: function () {
                    alert("Problem on uploading file");
                }

            });

        });
    });

这是控制器下用于加载局部视图的代码。

public ActionResult FileUpload()
{
  return PartialView("~/Views/Dashboard/FileUpload.cshtml");
}

我已设法运行此代码并显示部分视图,但我现在很难从该部分视图执行操作。

我添加了另一个动作Fileload

[HttpPost]
public ActionResult FileLoad()
        {
             //some codes to execute
        }

这将被这个局部视图调用

@model Payout.Models.FundTransfer

@using (Html.BeginForm("FileLoad", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        <input type="file" name="Filepath" id="Filepath" />
    </div>
        <div>
            <input type="submit" name="submit" value="Submit" />
        </div>
}

现在我不知道执行该操作的最佳方式是什么。你有什么建议吗?

【问题讨论】:

  • 你为什么不使用集成在MVC的方式,通过使用AjaxForm...
  • 你有样品可以让我按照你说的去做吗?
  • @st_stefanov 您不能将 MVC Ajax 帮助程序用于文件上传。
  • @Mannan Bahelim 如果我尝试仅传递从局部视图获取的文件路径并访问主视图上的文件,该怎么办?有可能吗?如果没有,你有其他选择吗?

标签: c# jquery asp.net-mvc dashboard


【解决方案1】:

你可以修改你的代码如下

$.ajax({
          url: '@Url.Action("FileUpload", "Dashboard")',
          type: "POST",
          processData: false,
          data: { type: $(this).attr("data-type") },
          dataType: "html",
          contentType: false,
          success: function (data) {
              $(".content").html(response);
          },
          error: function () {
              alert("Problem on uploading file");
          }
      });

【讨论】:

    【解决方案2】:

    那么这里提示的错误是什么?

    顺便说一句,对于 asp.net core,您的 post 操作应该是这样的;

    public ActionResult FileLoad(IFormFile Filepath)
    

    对于经典的 asp.net 或像这样;

    public ActionResult FileLoad(HttpPostedFileBase Filepath)
    

    【讨论】:

    • 我已经这样做了,但我的问题是关于在局部视图上执行操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    相关资源
    最近更新 更多