tags:

views:

29

answers:

1

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, where 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>
+1  A: 

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
}
Dan Atkinson
I'm pretty confused. At what point do you pass the information through to the Controller? Where do you define the MyModel class? How does the controller know that the MyModel parameter matches the Hidden Field?
Stacey
All the information that is to be sent to the server in the form post must be inside the form tags. The property `Foo` is inferred by ASP.NET MVC, but you could do `MyModel.Foo` or something like that.
Dan Atkinson
I'm sorry, this doesn't seem to work. I really don't see where you're getting anything from. Where do you create a new instance of the myModel class? Why does it magically accept a myModel object in the parameter list? I just don't see where any of it adds up. I'm sitting here trying it on my end and all I get is 'null'.
Stacey
ASP.NET MVC creates an instance of `MyModel` and populates the values (in this case `Foo`) based on the posted form data that is sent to the server. There's not really a lot to say about this process as it's all handled automatically by the default model binder.
Dan Atkinson
I think I'm starting to get it... so ASP.NET MVC will automatically inject any objects passed from the view into the controller - and if you parametrize something with its type, it'll assign it there?
Stacey
Thank you, I think this is starting to make sense. The work done by the model binder behind the scenes is what was confusing me.
Stacey
Well, they're not really objects. In this case, they're hidden form values. When you post the form, ASP.NET MVC will see that one of those form keys is `Foo` and will then look at what parameters are being used in action, and will then go about instantiating them with the values from the form data.
Dan Atkinson