简体   繁体   English

ASP.NET MVC 4表单发布变量在接收控制器中为null

[英]ASP.NET MVC 4 form post variables are null in receiving controller

I have this model: 我有这个模型:

  public class WorkflowImport {
    public bool IsLive { get; set; }
    public string DateTimeCreated { get; set; }
    public string CreatedByUser { get; set; }
    public string VersionComments { get; set; }
    public string VersionNumber { get; set; }
    public string FilePath { get; set; }

    public List<WorkflowProcess> WorkflowProcesses { get; set; }
  }

A partial view: 部分视图:

@model <FullyQualifiedPathTo...>.ViewModels.WorkflowImport

<div class="subSectionHeader">Upload New Workflow Profile</div>
<div class="AccountDetailLine">
  @using ( Html.BeginForm( "UploadNewMatrix", "Home", FormMethod.Post, new { enctype = "multipart/form-data" } ) ) {
    <table>
      <tr>
        <td>Select Local File:</td>
        <td>
          <div class="upload-wrapper">
            <input type="file" name="file" id="xlsfile" />
          </div>
        </td>
      </tr>
      <tr>
        <td>Version Comments:</td>
        <td>
          @Html.TextBox( "comments", Model.VersionComments, new Dictionary<string, object> { { "class", "textboxUploadField" } } )
        </td>
      </tr>
      <tr>
        <td>Version Number:</td>
        <td>
          @Html.TextBox( "version", Model.VersionNumber, new Dictionary<string, object> { { "class", "textboxUploadField" } } )
        </td>
      </tr>
      <tr>
        <td colspan="2"></td>
      </tr>
      <tr>
        <td>Select whether this file upload will update<br />
          the live workflow matrix or just the "What If" test matrix</td>
        <td>LIVE @Html.RadioButtonFor( model => model.IsLive, "True" )
          "What If" @Html.RadioButtonFor( model => model.IsLive, "False" )
        </td>
      </tr>
    </table>
    <div class="submit-wrapper">
      <input type="submit" value="Import Now" id="form_submit" class="ovobutton" />
    </div>
  }
</div>

and a receiving controller/method: 以及接收控制器/方法:

public class HomeController: Controller {
    // POST /Home/UploadNewMatrix
    [HttpPost]
    public ActionResult UploadNewMatrix( WorkflowImport workflowImport ) {
      return View( workflowImport );
    }
}

But when I enter some values into the two textboxes and click the submit button, I get null values in the bound object on the controller, when checking in the debugger. 但是,当我在两个文本框中输入一些值并单击“提交”按钮时,在检入调试器时,我在控制器上的绑定对象中得到了空值。

I don't know why this is happening, because I used an almost identical pattern (including file upload with "multipart/form-data") on a previous project and was able to get the values successfully. 我不知道为什么会这样,因为我在先前的项目中使用了几乎相同的模式(包括使用“ multipart / form-data”上传文件)并且能够成功获取值。

Is there something obvious I have not seen here? 有什么明显的我没在这里看到的吗? The difference between this new application and the previous is that it is a partial view in amongst a lot of jquery, but I don't see how that could make a difference. 这个新应用程序与以前的应用程序之间的区别在于,它是很多jquery中的局部视图,但是我不知道这会产生什么影响。 Also, I need to use traditional file upload as the target browser is IE8 and does not support HTML5. 另外,我需要使用传统的文件上传功能,因为目标浏览器是IE8,并且不支持HTML5。

For correct binding, name html element must equal property name 为了正确绑定,名称html元素必须等于属性名称

property: 属性:

  public string VersionComments { get; set; }

View: 视图:

   <tr>
        <td>Version Comments:</td>
        <td>
          @Html.TextBox("VersionComments", Model.VersionComments, new Dictionary<string, object> { { "class", "textboxUploadField" } } )
        </td>
      </tr>

Note: i agree with Jacob. 注意:我同意雅各布的观点。 Using @Html.EditorFor(m=>m.VersionComments) more flexible approach, but my example demonstrates binding principle. 使用@Html.EditorFor(m=>m.VersionComments)更灵活的方法,但是我的示例演示了绑定原理。

You're getting back null because the name of the inputs don't match what the model binder expects. 您将返回null,因为输入的name与模型绑定程序期望的不匹配。 To help you create the right names, ASP.NET MVC has some useful helper methods. 为了帮助您创建正确的名称,ASP.NET MVC提供了一些有用的帮助器方法。 Instead of: 代替:

@Html.TextBox(
    "comments", 
    Model.VersionComments, 
    new Dictionary<string, object> { { "class", "textboxUploadField" } } )

...do this instead: ...改为:

@Html.TextBoxFor(
    m => m.VersionComments, 
    new Dictionary<string, object> { { "class", "textboxUploadField" } } )

This will use the value of VersionComments , and it will also give the input the name VersionComments so that it knows to plug this into the model when posting. 这将使用VersionComments的值,并且还将为输入提供名称VersionComments以便在发布时知道将其插入模型中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM