【问题标题】:Asp.Net MVC html.DropDownList Action according to selected valueAsp.Net MVC html.DropDownList 根据所选值操作
【发布时间】:2016-04-17 06:33:27
【问题描述】:

我创建了一个带有公司名称 DropDownList 的投标 Web 应用程序。我从特定目录中获取这些名称,放入数组并复制到列表中,然后添加字符串“-Add new-”,以便在用户选择此选项时创建操作。

第一个问题:当用户选择“-Add new-”时,如何创建一个打开一个带有文本框的小窗口的操作,并且在输入名称并单击“Add”后,它会在其中创建一个新文件夹我的“\我的网络目录\”?

第二个问题:正如我之前所说,公司名称的DropDownList来自“\我的网络目录\”,我如何传递用户选择的值(公司名称)并在另一个DropDownList中显示子-选择的文件夹(公司)的目录?

//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>          

页面: It looks like this

【问题讨论】:

  • 这种情况下你应该使用javascript
  • 我不太擅长 javascript... 你能解释一下具体是怎么做的吗?
  • 你应该查看一些教程。喜欢this

标签: c# asp.net asp.net-mvc asp.net-mvc-4 html.dropdownlistfor


【解决方案1】:

第一个问题:

Run javascript after dropdownlist change.

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

那你可以create a button from the Javascript

点击“添加”后,在控制器中创建一个动作来创建新文件夹。

第二个问题:

与第一个问题相同的技巧,使用 onchange 事件来运行 Javascript。 在Javascript中你可以add values to dropdownlists

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 2011-12-26
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多