Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有部分视图的模式窗口(ASP.NET MVC)_C#_Jquery_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 具有部分视图的模式窗口(ASP.NET MVC)

C# 具有部分视图的模式窗口(ASP.NET MVC),c#,jquery,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Jquery,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我的视图中有一个模式窗口,它通过按钮显示 模态窗口的视图是局部视图 这是部分视图的代码 但当我点击保存按钮时,我得到了错误 参数字典包含方法'System.Web.Mvc.ActionResult索引(System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.St

我的视图中有一个模式窗口,它通过按钮显示

模态窗口的视图是局部视图

这是部分视图的代码

但当我点击保存按钮时,我得到了错误

参数字典包含方法'System.Web.Mvc.ActionResult索引(System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、Int32)的不可为空类型'System.Int32'的参数'id'的空项'在'SmartSolutions.Controller.QuestionsController'中'。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数名称:参数

当我在Post方法上设置断点时,它不会命中


那为什么呢?当我运行Post方法时,它必须生成id

您没有显示相关代码-错误是当您调用
QuestionController
Index()
方法时-我假设这发生在您的
window.location.href=da.RedirectUrl代码行。但是,如果您最终想要重定向,为什么要使用ajax来发布呢。只需进行正常提交(使用ajax是毫无意义的),但如果我想使用ajax?@Stephenmueckeseem您有一个
索引
操作方法,其中
id
参数声明为整数(
public ActionResult Index(…,int id)
),但
重定向URL
没有提供所需的id(检查
返回Json
部分)。它应该是
return RedirectToAction
,以正常形式提交(使用
Html.BeginForm
指向
CreateNewQuestion
操作方法)。为什么ajax的全部目的是保持在同一页面上,并提高性能。你的表演太差劲了!为什么你没有绑定到一个模型——你确实意识到MVC中的M代表模型。你需要给自己买一本好书,学习基础知识,因为我需要保持不变page@StephenMuecke
<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 () {
            save();
        });
    });
    function save() {
        $.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") {
                    window.location.href = da.RedirectUrl;
                } else {
                    alert('Error' + da.Message);
                }
            },
            error: function (da) {
                alert('Error');
            }
        });
    }
</script>
[HttpPost]
public ActionResult CreateNewQuestion(string Question_new, string Answer, string Preparing, string Retries)
{
    Question quest = new Question
    {
        question = Question_new,
        TimeForAnswer = Answer,
        TimeForReady = Preparing,
        Retries = Retries,
    };
    db.Questions.Add(quest);
    db.SaveChanges();

    return Json(new { Result = "Success", Message = "Saved Successfully"});
}