0

I'm new to ASP.NET MVC 4.

I'm able to create a folder with a hardcoded name using Directory.CreateDirectory(@"C:/") in my controller, but what I need to do is have the user type the desired folder name into a textbox, and pass that information to the CreateFolder method in the Controller.

Here's the method:

public ActionResult CreateFolder(String newFolderName)
    {
        Directory.CreateDirectory(@"C:\..." + newFolderName);

        return View();
    }

In my view I need a textbox for the user to define the desired folder name, and a button that creates the folder with the selected name. How should I handle this?

I've tried some suggestions from the web, but can't seem to get it going.

tereško
  • 58,060
  • 25
  • 98
  • 150
sabastienfyrre
  • 467
  • 1
  • 4
  • 14
  • _'I've tried some suggestions from the web'_ ... like what? Lest we reiterate. Furthermore, it is entirely unclear exactly what your problem actually is. – Grant Thomas Jun 10 '13 at 19:12
  • My spidey sense is going off about the fact that you're trying to tightly couple a web concept like MVC w/ a winforms concept like File Directories... Not saying there aren't some applications for it, but it makes me pause – Rikon Jun 10 '13 at 19:13
  • @Rikon: Directories are not a WinForms concept. However, this is probably a bad idea and may be a security hole. – SLaks Jun 10 '13 at 19:16
  • What problem are you having? – SLaks Jun 10 '13 at 19:20
  • Hello, I've tried integrating something akin to this. http://stackoverflow.com/questions/16064481/asp-net-mvc-4-calling-method-from-controller-by-button and http://stackoverflow.com/questions/15167635/asp-net-mvc-html-textbox-not-returning-value as well as a number of others. Apologies if I've been too vague. It was in the hope of seeing how others might handle it from a fresh perspective. – sabastienfyrre Jun 10 '13 at 19:22
  • To clarify, I'm new to MVC as a whole. I'm experimenting with some simple applications to get my bearings. – sabastienfyrre Jun 10 '13 at 19:24

1 Answers1

2

View:

@using Folder
@using ( @Html.BeginForm( "CreateFolder", "ControllerName", FormMethod.Post) )
{
    @Html.TextBoxFor(x=>x.FolderName)
    <input type="submit" id="btnCreateFolder" value="Create Folder" />
}

Model:

public class Folder
{
    // other properties
    string FolderName {get;set;}
} 

Controller:

[HttpPost]
public ActionResult CreateFolder(Folder model)
{
    Directory.CreateDirectory(@"C:\..." + model.FolderName);
    return View();
}
Xordal
  • 1,369
  • 13
  • 29