让模型回到Post

时间:2010-01-13 19:54:27

标签: asp.net-mvc

如果我不能将其作为参数传递,是否有任何方法可以在标记为HttpPost的结果中从模型中获取信息?

    [AcceptVerbs(HttpVerbs.Post)]
    public FileUploadJsonResult Upload(HttpPostedFileBase file, IwantMyModelToo! )
但是,我无法真正获得实际的视图模型来完成该方法。有什么想法吗?

这是主要观点。 (FoldersController)

    <hr class="space" />
    <div>
        <% Html.RenderAction<Controllers.ImagesController>(i => i.Create(Model)); %>
    </div>
    <hr class="space" />

这是局部视图(ImagesController,Create方法所在的位置)

// bunch of fun jQuery for jQuery Form Uploading.
</script>
<div class="span-24 last">
    <fieldset>
        <legend>Upload Image</legend>
        <form id="ajaxUploadForm" action="<%= Url.Action("Upload", "Images")%>" method="post" enctype="multipart/form-data" >
        <div>
            <label for="file">Select Image</label><br />
            <input type="file" name="file" />
        </div>
            <input id="ajaxUploadButton" type="submit" value="Upload" />
        </form>
    </fieldset>
</div>

1 个答案:

答案 0 :(得分:2)

在你的代码示例中没有连接到任何模型的属性......这里我在隐藏的表单字段中添加了一个(Foo),并创建了一个名为MyModel的类。

查看

<div class="span-24 last">
  <fieldset>
    <legend>Upload Image</legend>
    <form id="ajaxUploadForm" action="<%= Url.Action("Upload", "Images")%>" method="post" enctype="multipart/form-data" >
      <div>
        <%= Html.Hidden("Foo", "bar") %>
      </div>
      <div>
        <label for="file">Select Image</label><br />
        <input type="file" name="file" />
      </div>
      <input id="ajaxUploadButton" type="submit" value="Upload" />
    </form>
  </fieldset>
</div>

<强>模型

public class MyModel
{
  public string Foo {get;set;}
}

<强>控制器

public FileUploadJsonResult Upload(HttpPostedFileBase file, MyModel model)
{
  //model.Foo should be accessible here
}