DropDownList で選択した値を取得して、MVC 4 を使用してコントローラーに送信することについて多くの検索を行ったところ、次のようなものが見つかりました: 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 メソッドを使用してコントローラーにデータを送信したくありませんが、画像をアップロードしてコントローラーでアクションを呼び出すために使用される Aurigma という名前のプラグイン用の次のスクリプトを使用します。コードは次のとおりです。
<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();
そのため、2 つの DropDownLists IDFLIGHT と IDTYPE で選択した値の ID を取得し、それを actionURL に渡したいと考えています。だから、私はそのようなことをしたい:
actionUrl: '@Url.Action("Ajouter", "Image", new { IdFlight = "id for flight", IdType = "id for type"})'
ここに私のコントローラーがあります:
[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;
}
ありがとう !