在默认选择ASP.NET MVC

时间:2016-11-09 13:51:38

标签: asp.net-mvc validation postback viewmodel

ASP.NET MVC的新手,我以前遇到过这种情况,但我觉得我已经开始以不正确的方式修复它了。我希望有人能指出我正确的方法。

我有一个页面,用户选择下拉值并点击下一步。现在,如果他们没有选择项目并选择默认值(“选择...”),则会出现验证错误。即使模型在“回发”中返回到视图,控制器似乎也会丢失有关下拉列表的信息。所以,我基本上不会在我[HTTPPost]的{​​{1}}中重复代码。我收到错误:

[HTTPGet]

on

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'SheetIndex'.

因此,除非我重复代码,否则在没有选择时会出现错误。我做错了什么?

视图模型:

@Html.DropDownListFor(model => model.SheetIndex, Model.SheetsDropdown, "Select...", new { @class = "form-control" })

控制器:

public class SelectSheetViewModel
{
    public int? Id { get; set; }
    public string Name { get; set; }
    [Required]
    public string SheetIndex { get; set; }
    public string SheetName { get; set; }
    public int? ChainId { get; set; }
    public int? SheetId { get; set; }
    public int? FileId { get; set; }
    public IEnumerable<SelectListItem> SheetsDropdown { get; set; }
    public HeaderViewModel Header { get; set; }
}

查看:

[HttpGet]
public ActionResult SelectSheet(int? chainId, int? sheetId, int? fileId)
{
    if (sheetId == null || fileId == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var fileDetails = db.FileDetails.Find(fileId);

    SelectSheetViewModel selectSheet = new SelectSheetViewModel()
    {
        Id = fileDetails.FileId,
        Name = fileDetails.Name,
        ChainId = chainId,
        SheetId = sheetId,
        FileId = fileId
    };

    string fileName = fileDetails.UniqueName + fileDetails.Extension;
    string relativeFileLocation = "~/uploads/" + fileName;
    string absoluteFileLocation = HttpContext.Server.MapPath(relativeFileLocation);         

    if (System.IO.File.Exists(absoluteFileLocation))
    {
        DSDBuilder builder = new DSDBuilder();

        selectSheet.SheetsDropdown = builder.GetSheets(absoluteFileLocation); // Where I get my selectlist
    }
    else
    {
        ModelState.AddModelError("SheetDropdown", "Excel workbook does not exist.");
    }

    selectSheet.Header = BuildHeaderViewModel(chainId, sheetId);

    return View(selectSheet);
}

[HttpPost]
public ActionResult SelectSheet(SelectSheetViewModel selectSheet)
{
    if (ModelState.IsValid)
    {
        FileDetail fileDetails = db.FileDetails.Find(selectSheet.FileId);

        string[] sheetIndexAndName = selectSheet.SheetIndex.Split(':');

        fileDetails.SheetIndex = Convert.ToInt32(sheetIndexAndName[0]);
        fileDetails.SheetName = sheetIndexAndName[1];

        db.SaveChanges();

        return RedirectToAction("Build", "Sheets", new
        {
            ChainId = selectSheet.ChainId,
            SheetId = selectSheet.SheetId,
            FileId = selectSheet.FileId
        });
    }


    // Probably not a good method vvv
    var fileDetailsPostBack = db.FileDetails.Find(selectSheet.FileId);

    string fileName = fileDetailsPostBack.UniqueName + fileDetailsPostBack.Extension;
    string relativeFileLocation = "~/uploads/" + fileName;
    string absoluteFileLocation = HttpContext.Server.MapPath(relativeFileLocation);

    if (System.IO.File.Exists(absoluteFileLocation))
    {
        DSDBuilder builder = new DSDBuilder();

        selectSheet.SheetsDropdown = builder.GetSheets(absoluteFileLocation);
    }
    else
    {
        ModelState.AddModelError("SheetDropdown", "Excel workbook does not exist.");
    }
    // Probably not a good method ^^^

    return View(selectSheet);
}

我也不想重复该代码,因为它转到了一个使用Excel.Interop并且资源很多的函数。如果有人有更好的解决方案,请告诉我。我总是试图改进我的代码并以“正确”的方式做事。

0 个答案:

没有答案