开发者

Get Model back in Post

Is there any way to get information from my model during a result flagged HttpPost if I cannot pass it as a parameter?

    [AcceptVerbs(HttpVerbs.Post)]
    public FileUploadJsonResult Upload(HttpPostedFileBase file, IwantMyModelToo! )

I can't really get the actual view model to go through the method, though. Any thoughts?

Here is the primary view. (FoldersController)

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

Here is the partial view (ImagesController, wh开发者_如何转开发ere the Create method resides)

// 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>


In your code sample there are no properties connected to any model... Here I have added one (Foo) in a hidden form field, and created a class called MyModel.

View

<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>

Model

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

Controller

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜