Modal window with Partial View (ASP.NET MVC)

I have modal window in my View, it's displaying by button.

View for modal window is Partial View

Here is code of Partial View

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

I need to fill data and on save button write it table, so on Controller I have this code

[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"});
}

But when I click save button , I gets error.

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, Int32)' in 'SmartSolutions.Controllers.QuestionsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

When I set breakpoint on Post method it not hits.

SO why so? It must generate id when I run Post method

链接地址: http://www.djcxy.com/p/39214.html

上一篇: 通过POST(ajax)发送JSON数据并接收来自Controller(MVC)的json响应

下一篇: 部分视图模式窗口(ASP.NET MVC)