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.

6
  • 'I've tried some suggestions from the web' ... like what? Lest we reiterate. Furthermore, it is entirely unclear exactly what your problem actually is. Commented Jun 10, 2013 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 Commented Jun 10, 2013 at 19:13
  • @Rikon: Directories are not a WinForms concept. However, this is probably a bad idea and may be a security hole. Commented Jun 10, 2013 at 19:16
  • What problem are you having? Commented Jun 10, 2013 at 19:20
  • Hello, I've tried integrating something akin to this. stackoverflow.com/questions/16064481/… and stackoverflow.com/questions/15167635/… 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. Commented Jun 10, 2013 at 19:22

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, it's working perfectly now, and I've got a better understanding Controller - View communication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.