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

Javascript 更新下拉列表中的值(ASP.NET MVC),javascript,c#,jquery,asp.net,asp.net-mvc,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:c

我有下拉菜单和模式窗口

通过模态窗口,我向数据库中添加了新问题。下面是代码,我通过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>

$(文档).ready(函数(){
$(“#保存任务”)。单击(函数(){
savequestion();
});
});
// Сохранение вопроса в модальном окне
函数savequestion(){
$.ajax({
键入:“Post”,
数据类型:“Json”,
数据:{
新问题:$(“#问题”).val(),
答案:$('#答案').val(),
准备:$(“#准备”).val(),
重试次数:$(“#重试”).val(),
},
url:'@url.Action(“CreateNewQuestion”,“Questions”),
成功:功能(da){
如果(da.Result==“成功”){
$('#myModal').hide();
电子邮件_update();
}否则{
警报(“错误”+da.消息);
}
},
错误:函数(da){
警报(“错误”);
}
});
}
我想我有一些下拉列表,下面是代码

<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>

@Html.DropDownList(“Question1”,null,“jböПС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,“jböПС3”,htmlAttributes:new{@class=“form control”,@style=“height:40px;margin bottom:20px;”)
@Html.DropDownList(“Question4”,null,“jböПС4”,htmlAttributes:new{@class=“form control”,@style=“height:40px;margin bottom:20px;”)
@Html.DropDownList(“Question5”,null,“jböПС5”,htmlAttributes:new{@class=“form control”,@style=“height:40px;margin bottom:20px;”)
@Html.DropDownList(“Question6”,null,“jböПС6”,htmlAttributes:new{@class=“form control”,@style=“height:40px;margin bottom:20px;”)
@Html.DropDownList(“Question7”,null,“jböПС7”,htmlAttributes:new{@class=“form control”,@style=“height:40px;margin bottom:20px;”)
@Html.DropDownList(“Question8”,null,“jböПС8”,htmlAttributes:new{@class=“form control”,@style=“height:40px;margin bottom:20px;”)
@Html.DropDownList(“Question9”,null,“jböПС9”,htmlAttributes:new{@class=“form control”,@style=“height:40px;margin bottom:20px;”)
@Html.DropDownList(“Question10”,null,“jböПС10”,htmlAttributes:new{@class=“form control”,@style=“height:40px;margin bottom:20px;”)
我的问题是——当我添加新问题时,我在下拉列表中看不到它


要在下拉列表中查看新问题,我需要编写什么代码?

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

型号:

public部分类问题mastermodel
{
公共问题管理模型()
{
}
公共虚拟列表问题模型列表{get;set;}
}
公共部分类问题模型
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
控制器操作:

[HttpPost]
公共行动结果问题
{
QuestionsMasterModel objQuestionsMasterModel=新的QuestionsMasterModel();
objQuestionsMasterModel.QuestionsModelList=//在此处添加数据列表;
返回PartialView(“问题操作”,objQuestionsMasterModel);
}
视图应如下所示:

@model Application.model.QuestionsMasterModel
@使用(Html.BeginForm(“saveorupdatequestionmaster”、“QuestionController”、FormMethod.Post、new{@id=“QuestionsMasterForm”、@class=“form horizontal”}))
{
@Html.DropDownList(“问题1”,新选择列表(Model.QuestionsModelList,“Id”,“Name”),“-Select Certificate Type-”,新{@class=“form control”,@style=“height:40px;margin bottom:20px;”,placeholder=“lol”})
}
保存和更新应如下所示:

[HttpPost]
公共操作结果保存或更新请求主控(问题MasterModel objQuestionsMasterModel)
{
//将objQuestionsMasterModel数据保存到此处的数据库
objQuestionsMasterModel=//从该模型中的数据库中获取所有数据,以便在QuestionsAction视图中显示
返回PartialView(“问题操作”,objQuestionsMasterModel);
}

因为保存后不会重新加载视图。或者,您可以在save question method的ajax success(保存问题方法的ajax成功)中手动在下拉列表中添加值。您需要在
success
ajax之后手动将其添加到所有下拉列表中,或者如果您的下拉列表位于部分视图中,则在成功时重新加载部分视图,或者从操作结果返回部分视图。我如何才能做到这一点?@mmushtajax需要ajax吗解决方案