views:

26

answers:

1

I'm new to asp.net mvc, but I've thoroughly searched on this topic everywhere.

I've a ViewModel class named PictureViewModel which is the class an EditorTemplate view inherits from: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<div class="editor-label"><%: Html.LabelFor(m=>m.Name) %></div> 
<div class="editor-field"><%: Html.TextBox("form.Name") %></div> 

<div class="editor-label"><%: Html.LabelFor(m=>m.Description) %></div> 
<div class="editor-field"><%: Html.TextArea("form.Description") %></div> 

<% Html.RenderAction("SelectCategory", "Category"); %>

<div class="editor-label"><label for="thumbFile">Thumb</label></div> 
<div class="editor-field"><input id="thumbFile" name="thumbFile" type="file"/></div> 

<div class="editor-label"><label for="fullFile">Full</label></div> 
<div class="editor-field"><input id="fullFile" name="fullFile" type="file"/></div> 

Also I have file fields and all that is posted to itself

<%@ Page Title="" Language="C#" MasterPageFile="/Views/Shared/Site_Table.Master"
 Inherits="System.Web.Mvc.ViewPage<MvcApplicationVelarPolya.Areas.Admin.Models.PictureViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Create</h2>

    <%
  using (Html.BeginForm("create", "Pictures", FormMethod.Post, new {enctype="multipart/form-data"})) 
   {%> 

    <%: Html.EditorForModel() %>

    <input type="submit" name="Create" />

<% }%>

</asp:Content>

And here is the code devoted to handle the submit:

    [HttpPost]
    public ActionResult Create([Bind(Prefix="form")] PictureViewModel pm)
    {
        try
        {
            var p = new PictureViewModel();
            UpdateModel(p, "",new[] {"Name", "Description"});
            ViewData["name"] = Request.Files[0].FileName;
            return View("New", p);
        }
        catch
        {
            return View();
        }
    }

Neither adding prefixes, nor using FormCollection with UpdateModel doesn't help. I would greatly appreciate any help! Andrew.

Oh, yes, viewdata is popuated with filename correctly!

This is not working, too [HttpPost] public ActionResult Create(FormCollection fc/[Bind(Prefix="form")] PictureViewModel pm/) { try { var p = new PictureViewModel(); UpdateModel(p, "form",new[] {"Name", "Description"}); ViewData["name"] = Request.Files[0].FileName; // TODO: Add insert logic here return View("New", p); return RedirectToAction("Index"); } catch { return View(); } }

And this code

        foreach (var k in fc)
        {
            Debug.WriteLine(k.ToString());
        }

is producing

form.Name
form.Description
CategoryID
Create
A: 

In your editor template you should use strongly typed helpers (TextBoxFor and TextAreaFor) and you could get rid of magic strings and prefixes:

<div class="editor-label"><%: Html.LabelFor(m => m.Name) %></div> 
<div class="editor-field"><%: Html.TextBoxFor(m => m.Name) %></div> 

<div class="editor-label"><%: Html.LabelFor(m => m.Description) %></div> 
<div class="editor-field"><%: Html.TextAreaFor(m => m.Description) %></div> 

<% Html.RenderAction("SelectCategory", "Category"); %>

<div class="editor-label"><label for="thumbFile">Thumb</label></div> 
<div class="editor-field"><input id="thumbFile" name="thumbFile" type="file"/></div> 

<div class="editor-label"><label for="fullFile">Full</label></div> 
<div class="editor-field"><input id="fullFile" name="fullFile" type="file"/></div> 

To handle uploaded files you could add them either as view model properties or as action parameters:

public class PictureViewModel
{
    public string Name { get; set; }
    public string Description { get; set; }
    public HttpPostedFileBase ThumbFile { get; set; }
    public HttpPostedFileBase FullFile { get; set; }
}

...

[HttpPost]
public ActionResult Create(PictureViewModel pm)
{
    // Normally the pm variable should be fully 
    // initialized here from the POST request and you can use it directly
    // No need to call UpdateModel
    return View("New", pm);
}
Darin Dimitrov
Thanks a lot! It helped!
ABerezovskiy