asp.net mvc按钮单击确认对话框

时间:2016-01-22 05:19:58

标签: javascript jquery asp.net-mvc

我的按钮点击事件中未显示确认对话框。 如何在按钮点击时显示消息?代码没有按预期工作。

我在代码中遗漏了什么

这里我包含了我的代码

CSHTML

@model AjaxTest.Models.ProductModel
@{
    ViewBag.Title = "ProductAjax";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")" type="text/javascript"></script>

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>




<h2>ProductAjax</h2>
<div id="Productresult"></div>
<fieldset>
    <legend>Product</legend>
    @using (Ajax.BeginForm("ProductAjax", "Product", new AjaxOptions { Confirm = "Are you sure?",UpdateTargetId = "Productresult" }))
    {

        <div class="formRowContainer">
            <div class="labelContainer">Name</div>
            <div class="valueContainer">
                @Html.TextBoxFor(model => model.Name)
                @Html.ValidationMessage("Name")
            </div>
        </div>
        <div class="clearStyle"></div>
        <div class="formRowContainer">
            <div class="labelContainer">Description</div>
            <div class="valueContainer">@Html.TextBoxFor(model => model.Description)</div>
        </div>
        <div class="clearStyle"></div>
        <div class="formRowContainer">
            <div class="labelContainer">Manufacturer</div>
            <div class="valueContainer">@Html.TextBoxFor(model => model.Manufacturer)</div>
        </div>
        <div class="clearStyle"></div>
        <div class="formRowContainer">
            <div class="labelContainer">Price</div>
            <div class="valueContainer">
                @Html.TextBoxFor(model => model.BasePrice)
                @Html.ValidationMessage("BasePrice")
            </div>
        </div>
        <div class="clearStyle"></div>
        <div class="formRowContainer">
            <div class="labelContainer">Category</div>
            <div class="valueContainer">
                @Html.DropDownListFor(model => model.CategoryId, Model.Category, "--Select--")
            </div>
        </div>
        <div class="clearStyle"></div>
        <div class="buttonContainer">
            <button>Add Product</button>
        </div>
    }
</fieldset>

===========

控制器:

 public ActionResult ProductAjax()
    {
        ProductModel model = new ProductModel();
        return View(model);
    }
     [HttpPost]
        public ActionResult ProductAjax(ProductModel model)
        {
        if (ModelState.IsValid)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("Product Name :" + model.Name + "</br/>");
            sb.Append("Description :" + model.Description + "</br/>");
            sb.Append("Manufacturer :" + model.Manufacturer + "</br/>");
            sb.Append("Price :" + model.BasePrice + "</br/>");
            sb.Append("Category :" + model.Category[model.CategoryId-1].Text);
            return Content(sb.ToString());
        }
        else
        {
            return View(model);
        }
    }

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

0 个答案:

没有答案