mvc网站的2个按钮

时间:2016-08-09 09:53:41

标签: c# html asp.net-mvc

我的网页上有2个按钮,我遇到了问题。我想要两个按钮导入excel文件,我已经创建了后端代码。我已经设法让网站显示2个按钮,但是目前它们都执行相同的功能,而我更喜欢它,如果第一个按钮在importcontroller上执行MassImport方法而第二个按钮执行MassSPAImport。任何建议将不胜感激。

我的ViewModel用于2种不同的导入功能

namespace BLL.ViewModels.Import
{

        public class SPAMassImport
    {
        [Display(Name = "Mass Store Profile Allocation Data Import File (.csv)")]
        [Required(ErrorMessage = "Required")]
        public HttpPostedFileBase importFile { get; set; }
    }
}


    namespace BLL.ViewModels.Import
{
    /// <summary>
    /// ViewModel for Data Imports
    /// </summary>
    public class MassImport
    {
        [Display(Name = "Mass Import File (.csv)")]
        [Required(ErrorMessage = "Required")]
        public HttpPostedFileBase importFile { get; set; }
    }
}

我的控制器具有我想要执行的两个不同的功能,但我相信只有第一个被两个按钮使用。

namespace PopFit.Areas.CustomerArea.Controllers
{
    /// <summary>
    /// Controller for Data Imports
    /// </summary>
    [AuthAttribute]
    public class ImportController : Controller
    {
       /// <summary>
       /// Returns the index view
       /// </summary>
       /// <returns>ActionResult</returns>
        public ActionResult Index()
        {
            return View(new MassImport());
        }

        /// <summary>
        /// [HttpPost] Uploads and imports based on the MassImportDataDemo file
        /// </summary>
        /// <param name="file">HttpPostedFileBase CSV file</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult MassImport(MassImport vm)
        {
            if (ModelState.IsValid)
            {
                string type = vm.importFile.ContentType.ToLower().Trim();

                if (FileHelper.IsValidCsv(type))
                {
                    StoreImporter importer = new StoreImporter();                    
                    var result = importer.Import(vm.importFile);

                    switch (result)
                    {
                        case StoreImporterResult.FormatError:
                            //User needs to fix file format
                            ModelState.AddModelError("", "Incorrectly Formatted File");
                            break;
                        case StoreImporterResult.StoreCreationSaveFailed:
                            //Database/System error
                            ModelState.AddModelError("", "Failed To Save Store Data, please contact an administrator.");
                            TempData["Failure"] = "Import Failed, Please Review Your File and Try Again.";
                            break;
                        case StoreImporterResult.WindowSurfaceDataError:
                            //Window Surface data is invalid, format needs to be corrected
                            ModelState.AddModelError("", "There is an error with the Window Surface Data, please correct this and try again.");
                            TempData["Failure"] = "Import Failed, Please Review Your File and Try Again.";
                            break;
                        case StoreImporterResult.WindowSurfacesSaveFailed:
                            //Database/System error
                            ModelState.AddModelError("", "Failed To Save Window Surface Data, please contact an administrator.");
                            TempData["Failure"] = "Import Failed, Please Review Your File and Try Again.";
                            break;
                        case StoreImporterResult.ProfileAllocationSaveError:
                            //Database/System error
                            ModelState.AddModelError("", "Failed to save Profile Item Data, please contact an administrator.");
                            TempData["Failure"] = "Import Failed, Please Review Your File and Try Again.";
                            break;
                        case StoreImporterResult.WindowItemAllocationSaveError:
                            //Database/System error
                            ModelState.AddModelError("", "Failed to save Window Item Data, please contact an administrator.");
                            TempData["Failure"] = "Import Failed, Please Review Your File and Try Again.";
                            break;
                        default:
                            TempData["Success"] = "Import Successful";
                            break;
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "File must be a correctly formatted CSV");
                TempData["Failure"] = "Import Failed, Please Review Your File and Try Again.";
            }
            return View("Index", vm);
        }

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult MassSPAImport(MassImport vm)
        {
            if (ModelState.IsValid)
            {
                string type = vm.importFile.ContentType.ToLower().Trim();

                if (FileHelper.IsValidCsv(type))
                {
                    SPAStoreImporter importer = new SPAStoreImporter();
                    var result = importer.Import(vm.importFile);

                    switch (result)
                    {
                        case SPAStoreImporterResult.FormatError:
                            //User needs to fix file format
                            ModelState.AddModelError("", "Incorrectly Formatted File");
                            break;
                        case SPAStoreImporterResult.ProfileAllocationSaveError:
                            //Database/System error
                            ModelState.AddModelError("", "Failed to save Profile Item Data, please contact an administrator.");
                            TempData["Failure"] = "Import Failed, Please Review Your File and Try Again.";
                            break;
                        default:
                            TempData["Success"] = "Import Successful";
                            break;
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "File must be a correctly formatted CSV");
                TempData["Failure"] = "Import Failed, Please Review Your File and Try Again.";
            }
            return View("Index", vm);
        }
    }
}

我的索引视图

@using BLL.ViewModels.Import
@{ ViewBag.wrapClass = "admin-wrap"; } 
<div>
    <hgroup class="admin">
        <h2><i class="fa fa-lg fa-icon-for-pf-wrap"></i>Import.</h2>
    </hgroup>

    @using (Html.BeginForm("MassImport", "Import", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset class="fieldset">
            <legend class="legend">Mass Import Store Data</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.importFile)
            </div>
            <div class="editor-field" title="Must be formatted as per Mass Import Document.">
                <input name="importFile" id="importFile" type="file" class="admin" />
                @Html.ValidationMessageFor(model => model.importFile)
            </div>

            <div class="editor-label"></div>
            <div class="submit-container">
                <input type="submit" value="Import" class="submit" />
            </div>
        </fieldset>
    }

    @using (Html.BeginForm("ColMassImport", "Import", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset class="fieldset">
            <legend class="legend">Mass Import Store Profile Allocation Data</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.importFile)
            </div>
            <div class="editor-field" title="Must be formatted as per Mass Import Document.">
                <input name="importFile" id="importFile" type="file" class="admin" />
                @Html.ValidationMessageFor(model => model.importFile)
            </div>

            <div class="editor-label"></div>
            <div class="submit-container">
                <input type="submit" value="Import" class="submit" />
            </div>
        </fieldset>
    }

    <div class="admin">
        @Html.ActionLink("Back to Administration", "AdminMenu", "Index", null, new { @class = "anchor-icon back" })
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

您应该在索引视图中调用的方法是MassSPAImport,但是您正在调用ColMassImport,这可能是问题吗?