Asp.Net MVC html.根据所选值执行下拉列表操作

本文关键字:执行 下拉列表 操作 Net MVC html Asp | 更新日期: 2023-09-27 18:30:10

我创建了一个投标书的web应用程序,其中包含公司名称的DropDownList。这些名称我从一个特定的目录中提取,放入一个数组并复制到一个列表中,然后我添加了字符串"-Add-new-",以便在用户选择该选项时创建一个操作。

第一个问题:当用户选择"-Add-new-"时,我如何创建一个打开带有文本框的小窗口的操作,在写下名称并单击"添加"后,它会在我的"''my network directory''"中创建一个新文件夹?

第二个问题:正如我之前所说,公司名称的DropDownList来自"''My network directory''",我如何传递用户选择的值(公司名称),并在另一个DropDown列表中显示所选文件夹(公司)的子目录?

//The controller
public class TenderController : Controller
{
    //
    // GET: /Tender/
    public ActionResult AddNewTender()
    {
        //Get companies directory and put it into a string array
        string[] compNameArray = Directory.GetDirectories(@"//My network directory'");
        int i = 0;
        foreach (string txtName in compNameArray)
        {
            //Copy to another string every directory name only with the las name file (the company name)
            string txtDirName = txtName.Substring(txtName.LastIndexOf(@"'") + 1);
            //Update the companies name array with the companies name only
            compNameArray[i] = txtDirName;
            i++;
        }
        //Copy the companies name array to a list
        List<string> compList = new List<string>(compNameArray);
        //Remove from the list the names above
        compList.Remove("New folder");
        //Add the "add new" option to the list
        compList.Add("-Add new-");
        ViewBag.ListOfCompanies = compList;
        return View();
    }

视图:

            <td dir="rtl">
                @Html.DropDownList("companyName", new SelectList(ViewBag.ListOfCompanies, Model))
            </td>          

页面:看起来像这个

Asp.Net MVC html.根据所选值执行下拉列表操作

第一个问题:

在dropdownlist更改后运行javascript。

在Javascript中,您应该检查下拉值。

然后您可以从Javascript创建一个按钮。

单击"添加"后,在控制器中创建一个操作以创建新文件夹。

第二个问题:

与第一个问题相同的技巧是,使用onchange事件来运行Javascript。在Javascript中,您可以向下拉列表添加值。