実際に私のシナリオは次のとおりです。Excelからdbへのデータのインポート。ここでは、ビューバッグを介してコントローラからビューにデータセットを渡しています。次に、そのデータセットを POPUP のグリッドビューにバインドする必要があります。私のビューでは、ボタンが存在し、ボタンをクリックするとポップアップが表示されます。しかし、コントローラーからビューにビューを返している間、ボタンは表示されますが、ポップアップは表示されません。
私からしてみれば :
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>ImportExcel</h2>
<div align="center">
<% Html.DevExpress().Button(
settings =>
{
settings.Name = "btn_Import";
settings.Text = "Import Excel Sheet";
settings.UseSubmitBehavior = true;
}).Render(); %>
</div>
<%: Html.DevExpress().PopupControl(
settings =>
{
settings.Name = "pcModalMode";
settings.Width = 400;
settings.AllowDragging = true;
settings.CloseAction = CloseAction.CloseButton;
settings.PopupElementID = "btn_Import";
settings.PopupAnimationType = AnimationType.None;
settings.HeaderText = "Upload Email Addresses (XLS/XLSX)";
settings.Modal = true;
settings.PopupHorizontalAlign = PopupHorizontalAlign.WindowCenter;
settings.PopupVerticalAlign = PopupVerticalAlign.WindowCenter;
settings.SetContent(() =>
{
using (Html.BeginForm("ImportExcel", "PopUp", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
Html.Hidden("ID");
ViewContext.Writer.Write("<div class=\"line\">");
Html.DevExpress().Label(
labelSettings => {
labelSettings.Text = "Choose Your File:";
}
)
.Render();
Html.DevExpress().UploadControl(Usettings =>
{
Usettings.Name = "ImportData";
Usettings.CallbackRouteValues = new { Controller = "PopUp", Action = "Upload" };
Usettings.Width = Unit.Pixel(380);
Usettings.NullText = "Click here to browse files...";
Usettings.ShowProgressPanel = true;
}).Render();
ViewContext.Writer.Write( "</div><div class=\"line\">");
Html.DevExpress().Button(
buttonSettings => {
buttonSettings.Name = "btnUpload";
//buttonSettings.ControlStyle.CssClass = "button";
//buttonSettings.Width = 80;
buttonSettings.Text = "Upload";
buttonSettings.UseSubmitBehavior = true;
}
)
.Render();
Html.DevExpress().Button(
buttonSettings => {
buttonSettings.Name = "btnCancel";
//buttonSettings.ControlStyle.CssClass = "button";
buttonSettings.Width = 80;
buttonSettings.Text = "Cancel";
buttonSettings.ClientSideEvents.Click = "function(s, e){ pcModalMode.Hide(); }";
}
)
.Render();
ViewContext.Writer.Write("</div><b class=\"Clear\"></b>");
DataSet ds = (DataSet)ViewBag.imp;
}
});
settings.ClientSideEvents.CloseUp = "function(s, e){ ASPxClientEdit.ClearEditorsInContainer(null, '', true); }";
}).GetHtml()
%>
そして私のコントローラーは:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ImportExcel(FormCollection form)
{
UploadedFile[] files = UploadControlExtension.GetUploadedFiles("ImportData");
StringBuilder strValidations = new StringBuilder(string.Empty);
try
{
if (files[0].ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
Path.GetFileName(files[0].FileName));
files[0].SaveAs(filePath);
DataSet ds = new DataSet();
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("../Uploads/") + files[0].FileName + ";Extended Properties=Excel 12.0";
using (OleDbConnection conn = new OleDbConnection(ConnectionString))
{
conn.Open();
using (DataTable dtExcelSchema = conn.GetSchema("Tables"))
{
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
string query = "SELECT * FROM [" + sheetName + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
adapter.Fill(ds1, "Items");
ViewData["Mydataset"] = ds1;
ViewBag.imp = ds1;
ViewBag.text = "FIrst";
}
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
return View();
}