【问题标题】:Call a "create" partial view in Index view在索引视图中调用“创建”局部视图
【发布时间】:2019-08-05 18:42:06
【问题描述】:

我是 MVC Asp.Net 的新手,我正在处理一个简单的 Web 应用程序,但我正在尝试使用 Modals 对其进行动态编码(创建新记录),但问题是当我调用部分视图时,代码不保存信息。

这是模态框的 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);
    }

其实很简单,但是局部视图并没有保存任何东西。 我的代码有什么问题?

提前致谢。

【问题讨论】:

  • 您可以将您的控制器代码添加到帖子中吗?
  • 没问题。
  • 如果您在Create 发布操作中设置断点,它会被命中吗?我猜不是因为您的表单发布到Questions 操作.. 或者您可能只是没有发布相关代码,这很难说。从您发布的内容来看,您的表单 using 声明应该是 @using (Html.BeginForm("Create", "Questions", FormMethod.Post, new { id = "AddModal" }))

标签: c# asp.net asp.net-mvc web


【解决方案1】:

您在代码中的哪个位置调用了控制器的 Create 操作? 我认为你应该使用:

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

【讨论】:

  • 超级棒!非常感谢!
猜你喜欢
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 2021-03-14
  • 2018-01-12
  • 1970-01-01
相关资源
最近更新 更多