简体   繁体   English

使用强类型制作下拉菜单

[英]Using strongly typed to make a dropdown menu

I am trying to let the user choose between different names in a dropdown menu, used from a folder on the computer. 我试图让用户从计算机的文件夹中使用的下拉菜单中,在不同名称之间进行选择。 After they choose something they should be able to hit a button and then be able to edit the file. 选择了某些内容后,他们应该可以单击按钮,然后才能编辑文件。 I am trying to avoid just using ViewBag . 我试图避免只使用ViewBag

My controller (Which is returning the right data into a FormCollection (Should it be a FormCollection ?) 我的控制器(将正确的数据返回到FormCollection (应该是FormCollection吗?)

public ActionResult Index()
{
    FormCollection tableNames = TableNames();
    return View("Index",tableNames);
}

public FormCollection TableNames()
{
    String[] tableNamesPath = Directory.GetFiles(@"C:\Something\");
    FormCollection form = new FormCollection();

    foreach(String tableName in tableNamesPath)
    {
        form.Add(Path.GetFileName(tableName), Path.GetFileName(tableName));
    }

    return form;
}

Basicly I have nothing in my view, I have tried to use Html.DropDownList and Html.DropDownListFor (what is the difference?), but nothing seems to work. Html.DropDownList ,我认为我什么都没有,我尝试使用Html.DropDownListHtml.DropDownListFor (有什么区别?),但似乎没有任何效果。 It might just be something silly I am missing, however I would be very happy for some help. 我可能错过了一些愚蠢的事情,但是我很乐意提供帮助。

I would recommend you using a view model. 我建议您使用视图模型。 So start by defining one: 因此,首先定义一个:

public class TableViewModel
{
    public string SelectedItem { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

and then have your controller pass this view model to the view: 然后让您的控制器将此视图模型传递给视图:

public ActionResult Index()
{
    var tableNames = TableNames();
    return View("Index", tableNames);
}

public TableViewModel TableNames()
{
    String[] tableNamesPath = Directory.GetFiles(@"C:\Something\");
    TableViewModel model = new TableViewModel();
    model.Items = tableNamesPath.Select(item => new SelectListItem
    {
        Value = Path.GetFileName(item),
        Text = Path.GetFileName(item),
    }).ToList();

    return model;
}

and then make your view strongly typed to this view model and you will be able to use the strongly typed DropDownListFor helper: 然后将您的视图强类型化为该视图模型,就可以使用强类型的DropDownListFor帮助器:

@model TableViewModel 

@Html.DropDownListFor(x => x.SelectedItem, Model.Items)

If I understand you correctly you want to present a list of file names in a dropdownlist. 如果我对您的理解正确,则希望在下拉列表中显示文件名列表。 Let the user pick one, click a button and then present the contents of that file to the user for editing and saving. 让用户选择一个,单击一个按钮,然后将该文件的内容呈现给用户以进行编辑和保存。

If that is correct you should do a few things - 如果那是正确的,您应该做一些事情-

  1. Pass a SelectList with the names of the files to the view and populate the dropdownlist. 将带有文件名的SelectList传递给视图,并填充下拉列表。
  2. Send back the selected item and another an Edit (GET) action method responsible for returning an edit view. 发送回选定的项目和另一个负责返回编辑视图的Edit(GET)操作方法。
  3. Post back the edited data to the Edit (POST) action method and save the changes. 将编辑后的数据回发到“编辑(POST)”操作方法并保存更改。

If this is right, I'll post some code. 如果正确,我将发布一些代码。

First of all, passing a FormCollection is basicly wrong. 首先,传递FormCollection本质上是错误的。 The FormCollection is used to iterate through all post-values of a form. FormCollection用于遍历表单的所有后值。

I think your understanding of the basic approach of MVC is wrong. 我认为您对MVC基本方法的理解是错误的。 What you are trying to achive is editing a file. 您要达到的目的是编辑文件。 So your model should be a class called FileModel . 因此,您的模型应该是一个名为FileModel的类。 The controller should look like this: 控制器应如下所示:

[HttpGet]
public ActionResult Index()
{
    return View("Index", new FileModel());
}

This provides a view, bound to a new instance of FileModel . 这提供了绑定到FileModel的新实例的视图。 In the next step you want to select the file name of the file you want to edit. 在下一步中,您要选择要编辑的文件的文件名。 This means, your FileModel will get an property, called FileName , which get's assigned in the next step. 这意味着,您的FileModel将获得一个名为FileName的属性,该属性将在下一步中分配。

public class FileModel
{
    [Required]
    public string FileName { get; set; }
}

We now have the model and the controller . 现在我们有了模型控制器 Whats missing is the view part. 缺少的是视图部分。 This is the part that's actually responsible for user interaction. 这是实际上负责用户交互的部分。 How to select a file is not the responsibility of the controller, so building up your drop-down should be done from the view. 控制器不负责如何选择文件,因此应从视图中构建下拉列表。 In my opinion the controller shouldn't even know that a drop-down is used to select a file name. 在我看来,控制器甚至不应该知道使用下拉菜单来选择文件名。

@model FileModel

@using (Html.BeginForm())
{
    <fieldset>
        <ol>
            <li>
                @Html.LabelFor(m => m.FileName)
                @Html.DropDownListFor(m => m.FileName)
                @Html.ValidationMessageFor(m => m.FileName)
            </li>
        </ol>
        <input type="submit" value="Edit file" />
    </fieldset>
}

DropDownListFor simply tells the view, that the editor template for FileName is not a simple text box, but a drop-down. DropDownListFor只是告诉视图, FileName的编辑器模板不是一个简单的文本框,而是一个下拉列表。 Now the last step is to fill in the options for the drop-down. 现在,最后一步是填写下拉菜单的选项。 Therefore we can use an overload of DropDownListFor . 因此,我们可以使用DropDownListFor的重载。

@Html.DropDownListFor(m => m.FileName, 
    new SelectList(Directory.GetFiles(@"C:\Something\"))

Now you should be able to select a file from your directory. 现在您应该可以从目录中选择一个文件了。 This selection can be read in the controller from the post method: 可以从控制器的post方法中读取此选择:

[HttpPost]
public ActionResult Index(FileModel model)
{
    var selectedFileName = model.FileName;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM