【问题标题】:Putting an html.beginform inside a bootstrap modal将 html.beginform 放入引导模式
【发布时间】:2015-10-28 01:17:04
【问题描述】:

我想在 inside 一个引导模式中放置一个 html.beginform,该模式会发布到控制器操作方法。

引导模式正常运行,但是当我单击“保存”按钮时,它不会发布到我的方法中。

请注意,我的控制器 Edit 方法上装饰了正确的 ViewModel 名称和 HttpPost 方法。

请注意下图显示了左下角的链接,这似乎是一个“GET”(请参阅​​链接末尾的“2”):

在提交“保存”按钮后,我从 Fiddler 获得了以下信息。

GET http://localhost:7683/__browserLink/requestData/c7214b476210499781860a178a6b7607?version=2 HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:7683/Projects/Project/Edit/2?Name=A%26C+CaseTracker&Description=Keeps+track+of+all+dental+cases.&CustomerID=2&CategoryID=10&PriorityID=1&StatusID=4&Quote=509.00&Notes=Plus+maintenance+costs.
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: localhost:7683
DNT: 1
Connection: Keep-Alive

在我的“HTTPGET”方法上放置一个断点后,这就是它遇到的地方即使在我告诉它做一个 POST 的表单中:

@using (Html.BeginForm("Edit", "Project", FormMethod.Post))

谁能告诉我我做错了什么?

这是我的 html:

@model YeagerTechDB.Models.Project

@{
    ViewBag.Title = "Edit Project";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit Project</h2>

<div class="modal" id="projectEditModal" tabindex="-1" role="dialog" aria-labelledby="projectModal-label" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="projectModal-label">Edit Project: Project: @Model.ProjectID</h4>
            </div>
            <div class="modal-body">
                @using (Html.BeginForm("Edit", "Project", FormMethod.Post))
                {
                   <div class="form-group">
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        <div class="form-group">
                            @Html.LabelFor(model => model.Name, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "ProjectName" } })
                                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(model => model.Description, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control", @placeholder = "Description" } })
                                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(model => model.Customer.Email, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                @Html.DropDownListFor(model => model.CustomerID, new SelectList(ViewBag.Customers, "CustomerID", "Email", Model.CustomerID), "-- Select Customer --", new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.CustomerID, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(model => model.Category.CategoryDescription, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                                @Html.DropDownListFor(model => model.CategoryID, new SelectList(ViewBag.Categories, "CategoryID", "CategoryDescription", Model.CategoryID), "-- Select Category --", new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(model => model.Priority.PriorityDescription, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                                @Html.DropDownListFor(model => model.PriorityID, new SelectList(ViewBag.Priorities, "PriorityID", "PriorityDescription", Model.PriorityID), "-- Select Priority --", new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.PriorityID, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(model => model.Status.StatusDescription, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                                @Html.DropDownListFor(model => model.StatusID, new SelectList(ViewBag.Statuses, "StatusID", "StatusDescription", Model.StatusID), "-- Select Status --", new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.StatusID, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        @Html.LabelFor(model => model.Quote, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                        <div class="form-group">
                            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                @Html.EditorFor(model => model.Quote, new { htmlAttributes = new { @class = "form-control", @placeholder = "Quote" } })
                                @Html.ValidationMessageFor(model => model.Quote, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        @Html.LabelFor(model => model.Quote, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                        <div class="form-group">
                            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control", @placeholder = "Notes" } })
                                @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(model => model.CreatedDate, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                @Html.EditorFor(model => model.CreatedDate, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
                            </div>
                            @Html.LabelFor(model => model.UpdatedDate, new { @class = "control-label col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                @Html.DisplayFor(model => model.UpdatedDate, new { htmlAttributes = new { @class = "form-control" } })
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <a href="#" class="btn" data-dismiss="modal">Close</a>
                        <button type="submit" class="btn btn-success" id="btnSaveProject">Save</button>
                    </div>
                }
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script>
        $(document).ready(function ()
        {
            $('#projectEditModal').modal('show');
        });
    </script>
}

这是我的编辑帖子方法:

 [HttpPost]
               public async Task<ActionResult> Edit(Project project)
        {
            if (ModelState.IsValid)
            {
                //await db.EditProjectAsync(project);
            }

            List<CustomerDDL> customerList = await db.GetCustomerDDLAsync();
            ViewBag.Customers = customerList;

            List<Category> categoryList = await db.GetCategoriesAsync();
            ViewBag.Categories = categoryList;

            List<Priority> priorityList = await db.GetPrioritiesAsync();
            ViewBag.Priorities = priorityList;

            List<Status> statusList = await db.GetStatusesAsync();
            ViewBag.Statuses = statusList;

            return View(project);
        }

【问题讨论】:

  • 不确定到底是什么问题,但这里有一些问题可能有助于缩小范围: 1.) 向我们展示您的 MVC 路由配置是什么样的。浏览器中的 URL 是 localhost/Projects/Project/Edit 是不寻常的。在那条路径中有一个我通常看不到的额外部分。 2.) 你的 C# 控制器类的名称是什么? 3.) 你不会碰巧在我们可以查看的生产服务器上拥有此版本,对吗?
  • 您尚未向您显示主视图(您从中加载模式),但我怀疑其中可能还有一个表单标签。
  • Rajeev,关于#1,那是因为我在我的项目中使用了“区域”。因此,区域是“项目”,控制器是“项目”。对于#2,它是“ProjectController.cs”。对于#3,我确实在网站“YeagerTech.azurewebsites.net”上拥有它,但是,我能够使用 url.action 成功完成所有这些操作,它启动了 Ajax 请求并且工作正常。我想看看是否可以使用 BeginForm 而不是编写 Ajax 脚本。
  • Stephen,模式是通过 JS 在试图执行 BeginForm 的视图中加载的。 " $('#projectEditModal').modal('show');".
  • 底线:将 html.Beginform 放入 POST 的引导模式中是否可行。从我正在尝试做的事情来看,这似乎是不可能的。如果可能的话,我做错了什么。如果您需要任何其他信息,请告诉我,我很乐意提供...顺便说一句,您可以在网站上编辑数据,因为它只是测试数据。但是,同样,这没有 html.beginform。

标签: jquery html asp.net-mvc twitter-bootstrap


【解决方案1】:

您需要从div class="modal-footer" 中取出data-dismiss="modal"。我有同样的问题,这对我有用。希望它也适合你。

【讨论】:

    【解决方案2】:

    试试这个

     <h2>Edit Project</h2>
     @using (Html.BeginForm("Edit", "Project", FormMethod.Post))
                    {
    <div class="modal" id="projectEditModal" tabindex="-1" role="dialog" aria-labelledby="projectModal-label" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="projectModal-label">Edit Project: Project: @Model.ProjectID</h4>
                </div>
                <div class="modal-body">
    
                       <div class="form-group">
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                            <div class="form-group">
                                @Html.LabelFor(model => model.Name, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                                <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "ProjectName" } })
                                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.Description, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                                <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                                    @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control", @placeholder = "Description" } })
                                    @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.Customer.Email, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    @Html.DropDownListFor(model => model.CustomerID, new SelectList(ViewBag.Customers, "CustomerID", "Email", Model.CustomerID), "-- Select Customer --", new { @class = "form-control" })
                                    @Html.ValidationMessageFor(model => model.CustomerID, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.Category.CategoryDescription, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                                <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                                    @Html.DropDownListFor(model => model.CategoryID, new SelectList(ViewBag.Categories, "CategoryID", "CategoryDescription", Model.CategoryID), "-- Select Category --", new { @class = "form-control" })
                                    @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.Priority.PriorityDescription, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                                <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                                    @Html.DropDownListFor(model => model.PriorityID, new SelectList(ViewBag.Priorities, "PriorityID", "PriorityDescription", Model.PriorityID), "-- Select Priority --", new { @class = "form-control" })
                                    @Html.ValidationMessageFor(model => model.PriorityID, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.Status.StatusDescription, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                                <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                                    @Html.DropDownListFor(model => model.StatusID, new SelectList(ViewBag.Statuses, "StatusID", "StatusDescription", Model.StatusID), "-- Select Status --", new { @class = "form-control" })
                                    @Html.ValidationMessageFor(model => model.StatusID, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            @Html.LabelFor(model => model.Quote, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="form-group">
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    @Html.EditorFor(model => model.Quote, new { htmlAttributes = new { @class = "form-control", @placeholder = "Quote" } })
                                    @Html.ValidationMessageFor(model => model.Quote, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            @Html.LabelFor(model => model.Quote, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                            <div class="form-group">
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control", @placeholder = "Notes" } })
                                    @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.CreatedDate, new { @class = "control-label col-offset-1 col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                    @Html.EditorFor(model => model.CreatedDate, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
                                </div>
                                @Html.LabelFor(model => model.UpdatedDate, new { @class = "control-label col-lg-2 col-md-2 col-sm-2 col-xs-2" })
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                    @Html.DisplayFor(model => model.UpdatedDate, new { htmlAttributes = new { @class = "form-control" } })
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <a href="#" class="btn" data-dismiss="modal">Close</a>
                            <button type="submit" class="btn btn-success" id="btnSaveProject">Save</button>
                        </div>
    
                </div>
            </div>
        </div>
    </div>
     }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2013-03-02
    • 2014-11-09
    • 1970-01-01
    • 2023-03-20
    • 2020-12-19
    相关资源
    最近更新 更多