views:

45

answers:

2

Hi! 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.

+1  A: 

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.

BuildStarted
I am sure that names in form are the same as in model. I've changed FormCollection to Photo, but it doesn't help.
Agares
Did you set a breakpoint and validate that the collection has the appropriate values from the form post?
BuildStarted
+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).
eglasius
A: 

Check the html source in the browser.

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

eglasius
<input id="Description" name="Description" type="text" value="" /> - I think it's ok.
Agares
@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.
eglasius

related questions