表单提交时型号为NULL [英] Model is null when form submitted

查看:130
本文介绍了表单提交时型号为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击提交时,文件参数为null。

 公众的ActionResult的Create()
{
  返回查看(新FileViewModel());
}[HttpPost]
[InitializeBlobHelper]
公众的ActionResult创建(FileViewModel文件)
{
  如果(ModelState.IsValid)
  {
     //上传文件
  }
  其他
    返回查看(文件);
}公共类FileViewModel
{
  内部常量字符串UploadingUserNameKey =用户名;
  内部常量字符串FileNameKey =文件名;  内部常量字符串的文件夹=文件;  私人只读的Guid GUID = Guid.NewGuid();  公共字符串文件名
  {
    得到
    {
      如果(文件== NULL)
        返回null;
      VAR文件夹=文件夹;
      返回的String.Format({0} / {1} {2},文件夹,GUID Path.GetExtension(File.FileName))ToLowerInvariant()。
    }
  }  [RequiredValue]
  公共HttpPostedFileBase文件{搞定;组; }
}

下面是CSHTML:

  @model MyProject.Controllers.Admin.FileViewModel@ {
  ViewBag.Title =创建;
  布局=〜/查看/共享/ _BackOfficeLayout.cshtml
}@using(Html.BeginForm(创建,文件,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
  <&字段集GT;
    <传奇>创建< /传说>    < D​​IV CLASS =编辑标记>
      @ Html.LabelFor(型号=> model.File)
    < / DIV>
    < D​​IV CLASS =主编场>
      @ Html.TextBoxFor(型号=> model.File,新的{type =文件})
      @ Html.ValidationMessageFor(型号=> model.File)
    < / DIV>    &所述p为H.;
      <输入类型=提交值=创建/>
    &所述; / P>
  < /字段集>
}< D​​IV>
  @ Html.ActionLink(返回目录,索引)
< / DIV>


解决方案

它命名冲突和粘结剂尝试将文件属性FileViewModel的对象与文件名绑定,这就是为什么你会得到空。 POST名称是区分大小写的。

修改

 公众的ActionResult创建(FileViewModel文件)

要:

 公众的ActionResult创建(FileViewModel模型)

或任何其他名称

When I hit submit, the file parameter is null.

public ActionResult Create()
{
  return View(new FileViewModel());
}

[HttpPost]    
[InitializeBlobHelper]
public ActionResult Create(FileViewModel file)
{
  if (ModelState.IsValid)
  {
     //upload file
  }
  else
    return View(file);
}

public class FileViewModel
{
  internal const string UploadingUserNameKey = "UserName";
  internal const string FileNameKey = "FileName";

  internal const string Folder = "files";

  private readonly Guid guid = Guid.NewGuid();

  public string FileName
  {
    get
    {
      if (File == null)
        return null;
      var folder = Folder;
      return string.Format("{0}/{1}{2}", folder, guid, Path.GetExtension(File.FileName)).ToLowerInvariant();
    }
  }

  [RequiredValue]
  public HttpPostedFileBase File { get; set; }
}

Here is the cshtml:

@model MyProject.Controllers.Admin.FileViewModel

@{
  ViewBag.Title = "Create";
  Layout = "~/Views/Shared/_BackOfficeLayout.cshtml";
}

@using (Html.BeginForm("Create", "Files", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <fieldset>
    <legend>Create</legend>

    <div class="editor-label">
      @Html.LabelFor(model => model.File)
    </div>
    <div class="editor-field">
      @Html.TextBoxFor(model => model.File, new { type = "file" })
      @Html.ValidationMessageFor(model => model.File)
    </div>

    <p>
      <input type="submit" value="Create" />
    </p>
  </fieldset>
}

<div>
  @Html.ActionLink("Back to List", "Index")
</div>

解决方案

It's naming conflict and binder trying to bind your File property to FileViewModel object with file name, that's why you get null. POST names are case-insensitive.

Change:

public ActionResult Create(FileViewModel file)

To:

public ActionResult Create(FileViewModel model)

or to any other name

这篇关于表单提交时型号为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆