Вызовите «создать» частичное представление в представлении индекса - PullRequest
0 голосов
/ 14 марта 2019

Я новичок в MVC Asp.Net, я работаю над простым веб-приложением, но я пытаюсь динамически его кодировать, используя модалы (для создания новой записи), но проблема в том, что когда я вызываю частичное представление,Код не сохраняет информацию.

Это HTML-код модальности.

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                @Html.Partial("View", new MyWebAppInMVC.Models.Question())
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary">Save changes</button>
                            </div>
                        </div>
                    </div>
                </div>

Вот неполная страница:

@model MyWebAppInMVC.Models.Question

 @using (Html.BeginForm("Index", "Questions", FormMethod.Post, new { id = "AddModal" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Question</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DateOfQuestion, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DateOfQuestion, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.DateOfQuestion, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" id="BtnSubmit" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
     @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")}

Здесь контроллер

// GET: Questions/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: Questions/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,Title,Description,DateOfQuestion,Type")] Question question)
    {
        if (ModelState.IsValid)
        {
            db.Questions.Add(question);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(question);
    }

На самом деле все очень просто, однако частичное представление ничего не спасает.Что не так с моим кодом?

Заранее спасибо.

1 Ответ

0 голосов
/ 14 марта 2019

Где в вашем коде вы называете действие Create вашего контроллера?Я думаю, что вы должны использовать:

@using (Html.BeginForm("Create", "Questions", FormMethod.Post, new { id = "AddModal" }))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...