更新下拉列表中的值(ASP.NET MVC)

时间:2017-05-26 05:24:35

标签: javascript c# jquery asp.net asp.net-mvc

我有下拉菜单和模态窗口的视图

使用模态窗口我向数据库添加新问题这里是代码,我通过PartialView实现它:

    <script src="~/Scripts/jquery-3.1.1.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.js"></script>
<div>
    <div class="form-group" style="text-align:center;padding-bottom: 20px; padding-top: 10px;">
         <input type="text" class="form-control" id="question", placeholder="Вопрос" />
    </div>
    <div class="form-group" style="text-align:center;padding-bottom: 20px; padding-top: 10px;">
        <input type="text" class="form-control" id="answer" , placeholder="Время на ответ" />
    </div>
    <div class="form-group" style="text-align:center;padding-bottom: 20px; padding-top: 10px;">
        <input type="text" class="form-control" id="prepare" , placeholder="Время на подготовку" />
    </div>
    <div class="form-group" style="text-align:center;padding-bottom: 20px; padding-top: 10px;">
        <input type="text" class="form-control" id="retries" , placeholder="Попытки" />
    </div>
    <div class="form-group" style="text-align:center">
        <input type="button" id="save_quest" value="Создать" class="btn btn-default" style="margin-right: 40px;" />
    </div>
</div>
<script>
    $(document).ready(function () {
        $('#save_quest').click(function () {
           savequestion();
          });
    });

    // Сохранение вопроса в модальном окне
    function savequestion() {
        $.ajax({
            type: 'Post',
            dataType: 'Json',
            data: {
                Question_new: $('#question').val(),
                Answer: $('#answer').val(),
                Preparing: $('#prepare').val(),
                Retries: $('#retries').val(),
            },
            url: '@Url.Action("CreateNewQuestion", "Questions")',
            success: function (da) {
                if (da.Result === "Success") {
                    $('#myModal').hide();
                    emails_update();
                    } else {
                    alert('Error' + da.Message);
                }
            },
            error: function (da) {
                alert('Error');
            }
        });
    }
</script>

我查看我有一些下拉列表,这里是代码

<div class="listdivleft">
        <div style="height: 80%; width: 100%; overflow: auto">
            <div class="title2" style="margin-top: 15px; margin-left: 15px; margin-bottom: 15px; padding-top: 10px">
                @Html.DropDownList("Question1", null, "Вопрос 1", htmlAttributes: new {@class = "form-control", @style = "height:40px;margin-bottom: 20px;",placeholder="lol"})
                @Html.DropDownList("Question2", null, "Вопрос 2", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
                @Html.DropDownList("Question3", null, "Вопрос 3", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
                @Html.DropDownList("Question4", null, "Вопрос 4", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
                @Html.DropDownList("Question5", null, "Вопрос 5", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
                @Html.DropDownList("Question6", null, "Вопрос 6", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
                @Html.DropDownList("Question7", null, "Вопрос 7", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
                @Html.DropDownList("Question8", null, "Вопрос 8", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
                @Html.DropDownList("Question9", null, "Вопрос 9", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
                @Html.DropDownList("Question10", null, "Вопрос 10", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
            </div>
       </div>
        <input id="save" class="btn btn-default" type="button" style="margin-top:20px; margin-left:200px;" value="Сохранить" />
    </div>

我的问题 - 当我添加新问题时,我不会在下拉列表中看到它。

我需要编写代码才能在下拉列表中查看新问题?

1 个答案:

答案 0 :(得分:0)

您需要创建模型并将其传递给您的视图检查以下代码:

型号:

 public partial class QuestionsMasterModel
    {
        public QuestionsMasterModel()
        {
        }

        public virtual List<QuestionsModel> QuestionsModelList { get; set; }
    }
     public partial class QuestionsModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

控制器操作:

[HttpPost]
public ActionResult QuestionsAction()
{

QuestionsMasterModel objQuestionsMasterModel = new QuestionsMasterModel();
objQuestionsMasterModel.QuestionsModelList = //Add your List of data here;

return PartialView("QuestionsAction", objQuestionsMasterModel);
}

查看应该是这样的:

@model Application.Model.QuestionsMasterModel

@using (Html.BeginForm("SaveOrUpdateQuestionsMaster", "QuestionController", FormMethod.Post, new { @id = "QuestionsMasterForm", @class = "form-horizontal" }))
 {
     @Html.DropDownList("Question1", new SelectList(Model.QuestionsModelList, "Id", "Name"), "-Select Certificate Type-", new { @class = "form-control", @style = "height:40px;margin-bottom: 20px;", placeholder = "lol" })
  }

保存和更新应该是这样的:

 [HttpPost]
 public ActionResult SaveOrUpdateQuestionsMaster(QuestionsMasterModel objQuestionsMasterModel)
 {
      //Save objQuestionsMasterModel data to database here 
      objQuestionsMasterModel= //Feach all data from database in this model for show on the QuestionsAction view 
      return PartialView("QuestionsAction", objQuestionsMasterModel);
 }