【问题标题】:Html BeginForm is not passing the value from View to controller in ASP.Net MVC RazorHtml BeginForm 没有将值从 View 传递到 ASP.Net MVC Razor 中的控制器
【发布时间】:2014-02-19 16:44:10
【问题描述】:

在我的 .NET MVC3 razor PROJECT 中,我必须实现图像上传。我的问题是,当我点击上传按钮时,它只是刷新页面。

我的查看代码

@using (Html.BeginForm("UploadPhoto", "Home",FormMethod.Post, new { @enctype = "multipart/form-data" }))
                    {
                        <div class="form-group">
                        <label class="col-lg-2 control-label">
                            Customer ID</label>
                        <div class="col-lg-10">@Html.TextBoxFor(model => model.CusId, new { @class = "form-control" })</div>

                        <label class="col-lg-2 control-label">
                            Customer Name</label>
                        <div class="col-lg-10">@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })</div>
                        </div>
                        <input type="hidden" id="id" />
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-lg-2 control-label">
                                    DMIT Image</label>
                                <div class="col-lg-10">
                                     @Html.HiddenFor(model=>model.CusId)
                                     @Html.HiddenFor(model=>model.Name)
                                    <input type="file" id="file" name="file">
                                    <input type="submit" class="btn btn-success" value="Upload" />
                                </div>
                            </div>
                        </div>
                    }

这是我点击提交按钮时地址栏的变化

http:// /Home/FileUpload?CusId=1&Name=Nidheesh&CusId=1&Name=Nidheesh&file=Customer.JPG#

控制器

[HttpGet]
        public ActionResult UploadPhoto()
        {
            return View();
        }



        [HttpPost]
        public ActionResult UploadPhoto(ElixiCustPro elixi, HttpPostedFileBase uploadfile, int CusId,string Name)
        {
            try
            {
                if (uploadfile != null && uploadfile.ContentLength > 0)
                {
                    if ((uploadfile.ContentType == "image/jpeg") || (uploadfile.ContentType == "image/gif") || (uploadfile.ContentType == "image/png"))//check allow jpg, gif, png
                    {
                        elixi.Image = new byte[uploadfile.ContentLength];
                        uploadfile.InputStream.Read(elixi.Image, 0, uploadfile.ContentLength);
                        var filename = Path.GetFileName(uploadfile.FileName);
                        var path = Path.Combine(Server.MapPath("~/ElixirFiles/UploadImagesElixir/"), filename);
                        uploadfile.SaveAs(path);

                        ecp.Image = new byte[uploadfile.ContentLength];
                        ecp.ImageUrl = path;
                        ecp.CustomerName = Name;
                        ecp.CusId = CusId;
                        ment.ElixiProData.Add(ecp);
                        ment.SaveChanges();
                        return RedirectToAction("ScanManagement");
                    }
                }
            }
            catch (Exception ex)
            {
                return View(ex.Message.ToString());
            }


            return View();
        }

aspx.cs

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("favicon.ico");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Login", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

【问题讨论】:

  • 请求是否到达控制器?如果是,你能展示它的代码吗?
  • 不,它没有到达...我将显示我的控制器代码
  • 此外,您似乎有自定义路线。也很高兴见到他们
  • 当我点击上传按钮时,它会刷新页面并在地址栏中更改 http:// /Home/FileUpload?CusId=1&Name=Nidheesh&CusId=1&Name=Nidheesh&file=Customer.JPG#
  • 某事导致您的表单通过 GET 提交,即使您最初将其设置为 POST。由于 GET 操作只是返回视图,因此您永远无法到达任何地方。它可能是某个地方的一些流浪JS。

标签: asp.net asp.net-mvc image razor file-upload


【解决方案1】:

您的代码需要进行一些修改才能使其正常工作。

  1. 不需要像 - int CusId,string Name 这样的参数。它们已经包含在模型中。

  2. 您的 HttpPostedFileBase 名称应该是“文件”,它应该等同于 input=file 类型的名称。否则你会得到 null。

所以工作代码将是 -

[HttpPost]
public ActionResult UploadPhoto(ElixiCustPro elixi, HttpPostedFileBase file)
{
    return null;
}

我使用的其他部分代码如下 -

型号 -

public class ElixiCustPro
{
    public string Name { get; set; }
    public string CusId { get; set; }    
}

获取操作 -

[HttpGet]
public ActionResult UploadPhoto()
{
    ElixiCustPro p = new ElixiCustPro();
    return View(p);
}

查看 -

@model MVC.Controllers.ElixiCustPro

@using (Html.BeginForm("UploadPhoto", "Upload", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
    <div class="form-group">
        <label class="col-lg-2 control-label">
            Customer ID
        </label>
        <div class="col-lg-10">@Html.TextBoxFor(model => model.CusId, new { @class = "form-control" })</div>

        <label class="col-lg-2 control-label">
            Customer Name
        </label>
        <div class="col-lg-10">@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })</div>
    </div>
    <input type="hidden" id="id" />
    <div class="col-md-6">
        <div class="form-group">
            <label class="col-lg-2 control-label">
                DMIT Image
            </label>
            <div class="col-lg-10">
                @Html.HiddenFor(model => model.CusId)
                @Html.HiddenFor(model => model.Name)
                <input type="file" id="file" name="file">
                <input type="submit" class="btn btn-success" value="Upload" />
            </div>
        </div>
    </div>
}

输出 -

【讨论】:

    【解决方案2】:

    您需要对代码进行一些修改。

    1. “HttpPostedFileBase”参数名称与输入类型文件名相同。在您的情况下,您需要使用 file 作为“HttpPostedFileBase”参数名称。
    2. 如果您使用的是模型,则无需传递模型中已经存在的参数,例如 CusId, Name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      相关资源
      最近更新 更多