1

I gotta the following code in controller and view. The problem is that the model(Photo is an Entity Framework entity) is empty(all fields are nulled). Why?

// GET: /Admin/Photo/Create

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

    //
    // POST: /Admin/Photo/Create

    [HttpPost]
    public ActionResult Create(int id, FormCollection collection)
    {
        try
        {
            var file = (HttpPostedFileBase) Request.Files[0];
            if (file != null && file.FileName != null)
            {
                var filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Photos/Product/", Path.GetFileName(file.FileName));
                file.SaveAs(filename);
                var photo = new Photo();
                photo.Description = collection["Description"];
                photo.Main = collection["Main"].Contains("true");
                photo.Filename = Path.GetFileName(file.FileName);
                photo.Product_Id = id;
                Entities.AddToPhotos(photo);
                Entities.SaveChanges();
            }
            else
            { 
                ModelState.AddModelError("", "Plik musi zostać załadowany.");
                return View();
            }


            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

<h2>Create</h2>

<% using (Html.BeginForm(null, null, null, FormMethod.Post, new {enctype = "multipart/form-data" })) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Description) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Description) %>
            <%: Html.ValidationMessageFor(model => model.Description) %>
        </div>

        <div class="editor-label">
            <label for="MainContent_file">Plik: </label>
        </div>
        <div class="editor-field">
            <asp:FileUpload ID="file" runat="server" />
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Main) %>
        </div>
        <div class="editor-field">
            <%: Html.CheckBoxFor(model => model.Main) %>
            <%: Html.ValidationMessageFor(model => model.Main) %>
        </div>

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

<% } %>

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

Update: I checked and the collection is populated with proper fields, but they are all nulled.

4
  • Just a straight copy and paste with your code and html - with a model that contains only Description and Main - it works just fine. The problem might exist outside of this particular file. You're using an asp:FileUpload control. Try replacing it with an input. You'll have to remove the <form> tag in the masterpage, however. Commented Sep 24, 2010 at 17:23
  • Without the FileUpload control it works just fine. I don't have to remove <form> tag in the masterpage - it's Asp.net MVC, and i don't have <form> in masterpage. Commented Sep 24, 2010 at 17:35
  • Well, the reason I asked about the form tag is because a new project with asp:FileUpload tag throws an error that it requires the form tag with runat=server...so based on that a form tag surely exists, somewhere. Commented Sep 24, 2010 at 19:26
  • In general I would stay away from any server controls and just use plain html. try <input type="file" id="file" name="file" /> doing so I had no problems on my end. Commented Sep 24, 2010 at 19:27

2 Answers 2

1

Check to make sure that the name attributes in the resulting html match your collection name. You could also change your public ActionResult Create(int id, FormCollection collection) to public ActionResult Create(int id, YourViewModel model) to automagically map the post values to the model.

Sign up to request clarification or add additional context in comments.

3 Comments

I am sure that names in form are the same as in model. I've changed FormCollection to Photo, but it doesn't help.
Did you set a breakpoint and validate that the collection has the appropriate values from the form post?
+1 see my answer, if it is as I describe, the issue wouldn't have happened with automatic mapping. That said, the model of the OP doesn't seem to be a View Model, so its better not to use auto binding with it (I prefer to introduce the View Model with just the fields I'd want though).
0

Check the html source in the browser.

It might be sending them as: "Photo.Description"

2 Comments

<input id="Description" name="Description" type="text" value="" /> - I think it's ok.
@Agares didn't expect that, I suggest to check it with the debugger as BuildStarted suggested to see if the collection is coming populated with anything at all. As to why that would happen I'm not sure / post your findings in an update to the question, with the extra info someone might give you the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.