Agares
Agares

Reputation: 1262

Empty model when submitting asp.net mvc 2 form

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.

Upvotes: 1

Views: 1078

Answers (2)

eglasius
eglasius

Reputation: 36035

Check the html source in the browser.

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

Upvotes: 0

Buildstarted
Buildstarted

Reputation: 26689

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.

Upvotes: 1

Related Questions