sabastienfyrre
sabastienfyrre

Reputation: 487

Create a folder with a name typed by user in textbox ASP.NET MVC 4 Razor

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.

Upvotes: 0

Views: 1284

Answers (1)

Xordal
Xordal

Reputation: 1369

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();
}

Upvotes: 2

Related Questions