在ASP MVC 4中上传文件时的控制器方法导航

时间:2014-02-21 11:18:49

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

我正在尝试使用ASP .NET MVC 4上传文件。我完全遵循these指令,但是当我提交文件时,我总是发现自己使用了错误的索引方法(尽管呈现形式)。我的代码:

控制器:

public class ActivationController : Controller
{
    public ViewResult Index()
    {
        return View(new ActivationIndexViewModel());
    }

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        // NEVER GET HERE!
        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }
        return RedirectToAction("Index");  
    }
}

查看:

<form class="form-horizontal" role="form">
<div class="container-fluid">
    <div class="row">
        <div class="form-group">
            <label class="col-sm-2 control-label" for="file">Serial number</label>
            <div class="col-sm-10">
                @using (Html.BeginForm("Index", "Activation", FormMethod.Post, new { enctype = "multipart/form-data" }))
                {
                    <input type="file" name="file" />
                    <input type="submit" value="Load" class="btn btn-primary" />
                    <p class="help-block">Select a file</p>
                }
            </div>
        </div>
    </div>
    <div class="row">
        <div class="form-group">
            <label class="col-sm-2 control-label">Продукт</label>
            <div class="col-sm-10">
                @Html.DropDownListFor(m => m.SelectedProductId,
                    @Model.ActivatableProductsAsSelectList, new { @class = "form-control" })
                <p class="help-block">Select a product</p>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="form-group">
            <label class="col-sm-2 control-label">Options</label>
            <div class="col-sm-10">
                 <div class="table-responsive">
                    <table class="table"/>
                </div>
            </div>
        </div>
    </div>
</div>

RouteConfig

routes.MapRoute(
           name: "Activation",
           url: "{controller}/{action}",
           defaults: new { controller = "Activation", action = "Index" }
       );

我做错了什么?

1 个答案:

答案 0 :(得分:1)

好的,我发现了问题。问题是你的嵌套表格。 “提交”按钮将始终发布您的父表单,而您的父表单没有定义操作,方法或任何内容。所以它一次又一次地达到指数。

简单的解决方案是移除外形,或用内部形式替换外形。其他选项是使用AJAX表单,但不要嵌套它们。

你的观点是这样的 -

@using (Html.BeginForm("Index", "Activation", FormMethod.Post, new { enctype = "multipart/form-data", id = "Form1" }))
{
    <div class="container-fluid">
        <div class="row">
            <div class="form-group">
                <label class="col-sm-2 control-label" for="file">Serial number</label>
                <div class="col-sm-10">

                    <input type="file" name="file" />
                    <input type="submit" value="Load" class="btn btn-primary" />
                    <p class="help-block">Select a file</p>

                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label class="col-sm-2 control-label">Продукт</label>
                <div class="col-sm-10">
                    @Html.DropDownListFor(m => m.SelectedProductId, new SelectList(Model.ActivatableProductsAsSelectList, "Value", "Text"), "Select", new { @class = "form-control" })
                    <p class="help-block">Select a product</p>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label class="col-sm-2 control-label">Options</label>
                <div class="col-sm-10">
                    <div class="table-responsive">
                        <table class="table" />
                    </div>
                </div>
            </div>
        </div>
    </div>
}

然后你的控制器动作是 -

    [HttpPost]
    public ActionResult Index(ActivationIndexViewModel model, HttpPostedFileBase file)
    {
        // NEVER GET HERE!
        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }
        return RedirectToAction("Index");
    }

输出将是 -

enter image description here