简体   繁体   English

如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript?

[英]How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript?

I did many searches about getting the selected value in DropDownList to send it to the controller using MVC 4, and I found something like that: Request.Form["id"] but they didn't meet my needs. 我进行了很多搜索,以在DropDownList中获取选定的值以使用MVC 4将其发送到控制器,但我发现了类似的内容:Request.Form [“ id”],但它们不满足我的需求。 I have this form: 我有这个表格:

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

I don't want to send data to the controller using the Post Method in the form, but using the folowing Script which is for a plug-in named Aurigma used to upload images and to call an action in the Controller. 我不想使用表单中的Post方法将数据发送到控制器,而是使用以下脚本,该脚本用于名为Aurigma的插件,该插件用于上载图像并在Controller中调用操作。 Here is the code: 这是代码:

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

So, I want to get the id of the selected value in the two DropDownLists IDFLIGHT and IDTYPE and pass it in the actionURL. 因此,我想在两个DropDownLists IDFLIGHT和IDTYPE中获取所选值的ID,并将其传递到actionURL中。 So, I want to do something like that: 所以,我想做这样的事情:

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

and here is my 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;
    }

thank you ! 谢谢 !

@Url.Action() is razor code and parsed on the server before you pass it to the view. @Url.Action()是剃刀代码,在将其传递给视图之前已在服务器上进行了解析。 To build the url based on the selected values your dropdown lists, use 要根据下拉列表中的选定值构建url,请使用

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

暂无
暂无

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

相关问题 如何在JavaScript中将DropdownList Selected值与JavaScript传递给Asp.net MVC 4中的Controller? - How can I pass DropdownList Selected value with JavaScript to Controller in Asp.net MVC 4? 如何使用Javascript从asp.net的下拉列表中获取选定的值? - How to get selected value from dropdownlist in asp.net using Javascript? 使用Javascript在asp.net 4.5中下拉列表选择的值 - Dropdownlist selected value in asp.net 4.5 using Javascript 如何使用JavaScript从ASP.Net MVC中的表单获取“验证消息”的值? - How can I get the value of Validation Message from form in ASP.Net MVC, using javascript? 我如何在不使用javascript和asp.net选择dropdownlist值的情况下显示验证 - how can i show validation on without selecting dropdownlist value using javascript and asp.net 如何在 ASP.NET Controller 中发送客户价值? - How Can I send Clients value in ASP.NET Controller? 我如何从Asp.Net(selectedIndexChange)中的用户获取DropDownList选定的值 - How do I get DropDownList selected value from user in Asp.Net(selectedIndexChange) 如何在asp.net中使用jQuery获取下拉列表选择的值绑定? - How to get dropdownlist selected value bind using jquery in asp.net? 无法在ASP.NET MVC中的DropDownList中检索选定的值? - Unable to retrieve selected value in DropDownList in ASP.NET MVC? 我可以使用javascript / jquery绑定asp.net下拉列表吗? - Can I bind asp.net dropdownlist using javascript/jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM