javascript - 如何获取 DropDownList 选定值并将其发送到 ASP.NET MVC 4 中的 Controller 并使用 JavaScript?

标签 javascript c# html asp.net-mvc-4 aurigma

我做了很多关于获取 DropDownList 中的选定值以使用 MVC 4 将其发送到 Controller 的搜索,我发现了类似的内容:Request.Form["id"] 但它们不满足我的需求。 我有这个表格:

                        <div class="editor-label">
                            @Html.LabelFor(model => model.IDTYPE, "TYPES")
                        </div>
                        <div class="editor-field">
                            @Html.DropDownList("IDTYPE", String.Empty)
                            @Html.ValidationMessageFor(model => model.IDTYPE)
                        </div>

                        <div class="editor-label">
                            @Html.LabelFor(model => model.IDFLIGHT, "FLIGHTS")
                        </div>
                        <div class="editor-field">
                            @Html.DropDownList("IDFLIGHT", String.Empty)
                            @Html.ValidationMessageFor(model => model.IDFLIGHT)
                            @Html.ActionLink("Add New flight","Create","Flight",null,new{@style="font-size:16px;", @class="popup"})
                        </div>

我不想使用表单中的 Post 方法将数据发送到 Controller ,而是使用以下脚本,该脚本适用于名为 Aurigma 的插件,用于上传图像并调用 Controller 中的操作。 这是代码:

<script type="text/javascript">

        var uploader = $au.imageUploaderFlash({
            id: 'Uploader1',
            licenseKey: '77FF4-00485-962F7-E117E-11414-86DBBB',
            folderProcessingMode: 'Upload',
            restrictions: { fileMask: '*.jpg;*.jpeg;*.png;*.gif;*.bmp' },
            messages: { fileNameNotAllowed: 'You can add only images.' },
            width: '100%',
            height: '400px',
            converters: [
           { mode: '*.*=SourceFile' }
            ],
            uploadSettings: {
                actionUrl: '@Url.Action("Ajouter", "Image")'
    }
});
uploader.writeHtml();

所以,我想获取IDFLIGHT和IDTYPE这两个DropDownList中所选值的id,并将其传递到actionURL中。所以,我想做这样的事情:

actionUrl: '@Url.Action("Ajouter", "Image", new { IdFlight = "id for flight", IdType = "id for type"})'

这是我的 Controller :

[HttpPost]
    public ActionResult Ajouter(int idflight, int idtype)
    {
        for (int i = 0;
                i < Convert.ToInt32(Request.Form["PackageFileCount"]);
                i++)
        {
            if (Request.Form["File0Mode_" + i] != "sourceFile")
                throw
                  new Exception("Uploader expects to send original files.");
            HttpPostedFileBase sourceFile;
            sourceFile = Request.Files["File0_" + i];

            Char[] sepDCap = { '_', '.' };
            String[] data = sourceFile.FileName.Split(sepDCap);
            string Jour = data[1];
            string Mois = data[2];
            string Annee = data[3];

            string dateCreat = DateTime.Now.ToString();
            Char[] sepTDate = { '-', '/',' ' };
            String[] dateCr = dateCreat.Split(sepTDate);
            string Jourdc = dateCr[0];
            string Moisdc = dateCr[1];
            string Anneedc = dateCr[2];

            string pathToCreate = "~/Uploads/" + Anneedc + "/" + Moisdc + "/" + Jourdc;
            if (!Directory.Exists(Server.MapPath(pathToCreate)))
            {
                Directory.CreateDirectory(Server.MapPath(pathToCreate));
            }
            string dir = Server.MapPath(pathToCreate);

            sourceFile.SaveAs(dir + "/" + sourceFile.FileName);

            DateTime dateCapt = Convert.ToDateTime(Jour + "-" + Mois + "-" + Annee);
            double latitude = Convert.ToDouble(data[4]);
            double altitude = Convert.ToDouble(data[5]);
            string pathImage = pathToCreate;
            double longitude = Convert.ToDouble(data[6]);

            IMAGES img=new IMAGES();

            img.IDFLIGHT = idflight;
            img.IDTYPE = idtype;                
            img.DATECAPTURE = dateCapt;
            img.LATITUDE = latitude;
            img.ALTITUDE = altitude;
            img.PATHIMAGE = pathImage;
            img.CREATED = Convert.ToDateTime(dateCreat);
            img.LONGITUDE = longitude;

            ImageService imgSce = new ImageService();
            imgSce.create(img);
        }
        Response.Write("Upload Complete");
        return null;
    }

谢谢!

最佳答案

@Url.Action() 是 razor 代码,在将其传递给 View 之前在服务器上进行解析。要根据下拉列表中选定的值构建 url,请使用

 actionUrl: '@Url.Action("Ajouter", "Image")' + '?idflight=' + $('#IDFLIGHT').val() + '&idtype=' + $('#IDTYPE').val()

关于javascript - 如何获取 DropDownList 选定值并将其发送到 ASP.NET MVC 4 中的 Controller 并使用 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121398/

相关文章:

javascript - 如何使用 Google Maps API v3 更新已创建的 map

c# - Enum.GetValues() 是否使用反射?

C# 窗体 : Scrollbars without AutoScroll

html - Safari 不会通过 HTTPS 播放 mp4

javascript - React Native 有 'Virtual DOM' 吗?

javascript - 如何检查 jsgrid 中插入/编辑的项目是否重复?

javascript - React 中如何制作有焦点或有内容时可以向上移动的 float 标签?

c# - 更改 XML 节点值

html - 图片填入div(ruby)?

html - 下雪的麻烦