Dropdownlist mvc:如何设置值并保存在数据库中

时间:2017-02-07 15:23:28

标签: c# asp.net-mvc html.dropdownlistfor

我第一次在MVC Dropdownlist上工作。我可以从数据库填充下拉列表值,但我的问题是保存功能,在控制器中的httppost上,所选值需要保存在数据库中,我目前卡在此处。这是我的观点和控制器代码:

查看:

@Html.DropDownListFor(m => m.DisplayNumber, new SelectList(Model.DropdownQuestions.Where(m => m.question_part_number == 0), "DisplayNumber", "ValueDescription", 1, "---Select One---"))

@Html.ValidationMessageFor(m => m.ValDescription, "", new { @class = "text-danger" })

控制器:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(HsipDropdownQuestionsViewModel model)
    {
        try
        {

            string cRegion = "00";
            string cStateCode = "05";
            short nFY = 2016;
            decimal reportId = 1;
            decimal sectionId = 1;
            decimal subsectionId = 1;
            decimal questionNumber = 16;
            decimal questionPartNumber = 0;
            decimal displayNumber = 21;
            decimal colNumber = 1;
            string userId = string.Empty;



            if (ModelState.IsValid)
            {

                decimal? rowNumber = model.DisplayNumber;
                model.ValDescription = model.DropdownQuestions.First(p => p.DisplayNumber == model.DisplayNumber).ValueDescription;
                string response = model.ValDescription;


                hsipdropdownRepository.SaveDropdownValues(cRegion, cStateCode, nFY, reportId, sectionId, subsectionId, displayNumber, questionNumber, questionPartNumber, rowNumber, colNumber, userId, response);

                return RedirectToAction("Index", "DropdownQuestions");
            }

            return Content("saved");
        }
        catch (Exception e)
        {
            return null;
        }

    }

这里的问题是Save响应方法来自存储的proc结果。 这里的值描述和显示编号需要填充id值和下拉列表选择值,以便它可以将其保存到数据库中。我不确定为什么它无法从模型中获取值。

有人能指出我正确的方向。

谢谢, 哈

0 个答案:

没有答案