views:

30

answers:

1

Following the example here:

http://www.highoncoding.com/Articles/689_Uploading_and_Displaying_Files_Using_ASP_NET_MVC_Framework.aspx

I have an .ascx file I'm using as an editor template, and I would like to use this method to upload files, my .ascx file looks like so

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MHNHub.Models.ScrollerLink>" %>
<script type="text/javascript">
    $(function () {
        $("#dialog").dialog({
            bgiframe: true,
            height: 160,
            width: 400,
            modal: true,
            autoOpen: false,
            resizable: true
        })
    });

    </script>

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

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

<div class="editor-label">
    Current Image
</div>
<img src="<%: Model.ImageUrl %>" />
<div class="editor-field">
     <div id="dialog" title="Upload Image">
         <% using (Html.BeginForm("Upload", "HomeScroller", FormMethod.Post, new { enctype="multipart/form-data" }))
    { %> 

  Select a file: <input type="file" name="fileUpload" />   

  <input type="submit" value="Upload" />

  <% } %>
    </div>
    <a href="#" onclick="jQuery('#dialog').dialog('open'); return false">Upload Image</a>
</div>

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

My controller is called HomeScrollerController and the action method is called Upload, my controller and relevent actions are shown below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MHNHub.Models;
using MHNHub.ViewModels;
using System.Text.RegularExpressions;
using System.IO;

namespace MHNHub.Controllers
{
    public class HomeScrollerController : Controller
    {


             //
    // GET: /HomeScroller/Create

    public ActionResult Create()
    {
        var scrollerLink = new ScrollerLink();



        var viewModel = new HomeScrollerViewModel()
        {
            ScrollerLink = scrollerLink    
        };

        return View(viewModel);
    }

    private ScrollerLink GenerateScrollerLinkImageID(ScrollerLink scrollerLink)
    {
        var scrollerLinkList = dataContext.ScrollerLinks.ToList();

        int count = 1;

        foreach (var s in scrollerLinkList)
        {
            //Loop through the scroller link items and fix their ImageID, to remove any gaps if anything has been deleted
            s.ImageID = "slide-img-" + count;
            count++;
        }
        //Set this scrollerLinks ImageID to the last in the list.
        scrollerLink.ImageID = "slide-img-" + count;

        return scrollerLink;
    }

    public ActionResult Upload()
    {
        HttpPostedFileBase file = Request.Files["OriginalLocation"];
        HomeScrollerViewModel viewModel;
        Regex imageFilenameRegex = new Regex(@"(.*?)\.(jpg|jpeg|png|gif)$");
        ScrollerLink scrollerLink = new ScrollerLink();
        if (file.ContentLength > 0)
        {
            if (!imageFilenameRegex.IsMatch(file.FileName))
            {
                viewModel = new HomeScrollerViewModel()
                {
                    ScrollerLink = scrollerLink,
                    HasError = true,
                    ErrorMessage = "Image must be .jpg, .jpeg, .png, or .gif"
                };
            }
            else
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));
                file.SaveAs(filePath);
                viewModel = new HomeScrollerViewModel()
                {
                    ScrollerLink = new ScrollerLink()
                    {
                        SlideID = scrollerLink.SlideID,
                        ImageID = scrollerLink.ImageID,
                        ImageUrl = filePath,
                        ImageAlt = scrollerLink.ImageAlt,
                        ContentTitle = scrollerLink.ContentTitle,
                        ContentDescription = scrollerLink.ContentDescription,
                        ImageLink = scrollerLink.ImageLink
                    }
                };
            }
        }
        else
        {
            viewModel = new HomeScrollerViewModel()
            {
                ScrollerLink = scrollerLink,
                HasError = true,
                ErrorMessage = "Image is empty!? Try Again"
            };
        }
        return View(viewModel);
    }

    //
    // POST: /HomeScroller/Create

    [HttpPost]
    public ActionResult Create(ScrollerLink scrollerLink)
    {
        try
        {
            scrollerLink = GenerateScrollerLinkImageID(scrollerLink);

            dataContext.ScrollerLinks.InsertOnSubmit(scrollerLink);
            dataContext.SubmitChanges();

            return RedirectToAction("Index");
        }
        catch(Exception ex)
        {
            var viewModel = new HomeScrollerViewModel()
            {
                ScrollerLink = scrollerLink,
                HasError = true,
                ErrorMessage = ex.Message
            };

            return View(viewModel);
        }
    }
        }
    }

The issue that I am having is that my action method Isn't being called when the submit button on the form is clicked. I have tried this example from a new project(but not on an .ascx) and it works. I've spent far too much time on this and need to move forward, I'm new to MVC, with a Web Forms background. Your help is appreciated! Any Ideas Stack Overflow?

A: 

The issue was a nested form. The user control and its form was nested inside of another form on my .aspx page. The action fires correctly now.

Gallen