0

我试图在 javascript url 中传递 Html.Textbox 值,但它给出了错误。

Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /WebProduct/Add/1

下面是我的视图类。我从中将值传递给我的控制器。

已编辑

 @model IEnumerable<DatabaseService_WebAPI.Models.ProductType>

@{
    ViewBag.Title = "Tablets";

    <script type="text/javascript">

        $(function () {
            $('#edit').click(function () {
                var name = $('#quantity').val();
                this.href = this.href + '&quantity=' + encodeURIComponent(name);
            });
        });

    </script>

}

<h2>Tablets</h2>
@using (Html.BeginForm("Add", "WebProduct", FormMethod.Post))
{
    @Html.ValidationSummary(true)

    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Price)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Batch)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Expiry)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Quantity)
            </th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Batch)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Expiry)
                </td>
                <td>
                    @Html.TextBox("quantity")
                </td>
                <td>
                    @Html.ActionLink("Add", "Add", new { name = item.Name, type = item.Type }, new { id = "edit" })

                </td>
            </tr>
        }

    </table>
}
<div>
    @Html.ActionLink("Back to List", "Create")
</div>

它是我传递值的控制器方法。

[HttpPost]
        public ActionResult Add(string quantity, string name, string type)
        {

            Product product = new Product();
            if (type=="Tablet")
            {

                //string order = type.Name + " " + type.Quantity;
                LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name);

                product.city = tobj.City;
                product.OrderDate = DateTime.Now.Date.ToShortDateString();
                product.ShopName = tobj.ShopName;
                product.User = tobj.User;
                //product.OrderDetail = order;

                db.Products.Add(product);
                db.SaveChanges();

                return RedirectToAction("TypeT", "WebProduct");
            }
            else if (type == "Syrup")
            {
                //string order = type.Name + " " + type.Quantity;
                LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name);
                product.city = tobj.City;
                product.OrderDate = DateTime.Now.Date.ToShortDateString();
                product.ShopName = tobj.ShopName;
                product.User = tobj.User;
             //   product.OrderDetail = order;

                db.Products.Add(product);
                db.SaveChanges();

                return RedirectToAction("TypeS", "WebProduct");
            }
            else
            {

              //  string order = type.Name + " " + type.Quantity;
                LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name);
                product.city = tobj.City;
                product.OrderDate = DateTime.Now.Date.ToShortDateString();
                product.ShopName = tobj.ShopName;
                product.User = tobj.User;
             //   product.OrderDetail = order;

                db.Products.Add(product);
                db.SaveChanges();

                return RedirectToAction("TypeC", "WebProduct");
            }

            return View();
        }

此时我不想使用按钮选项。因为我想将数据库记录和用户的输入发送到我的控制器的方法。

4

3 回答 3

0

线

 this.href = this.href + '?quantity=' + encodeURIComponent(name);

您的网址将显示为

http://localhost:54745/WebProduct/Add?id=1&name=myname&type=mytype?quantity=4

两个问号不能像这样在同一个 url 查询字符串中

更新:您也在 foreach 循环中,这意味着您将有多个带有 id 'quantity' 的文本框和多个带有 id 'edit' 的添加链接

更新 2:在点击事件中添加参数 'e' 以调用 'e.preventDefault()' 这将阻止链接转到其 URL。那么您还需要设置 window.location,而不是 this.href(这是链接 URL)

    $(function () {
        $('#edit').click(function (e) {
            e.preventDefault();
            var name = $('#quantity').val();
            window.location = this.href + '&quantity=' + encodeURIComponent(name);
        });
    });

</script>
于 2012-11-29T06:04:13.277 回答
0

我的理解是你的

this.href == localhost:3325/WebProduct/Add?name=Panadol&type=Tablet

如果这是正确的,那么以下应该有效

$(function () 
 {
    $('#edit').click(function () 
    {
        var name = $('#quantity').val();

        var href = this.href;
        var splitHalfUrl = href.split('?');
        var splitQueryString = splitHalfUrl[1].split('&');
        window.location = "/WebProduct/Add?" + "quantity=" + encodeURIComponent(name) // Quantity
                                            "&" + splitQueryString[0] +   // Name
                                            "&" + splitQueryString[1];    // Type

     });
   });
于 2012-11-29T07:07:16.550 回答
0

好的,我想指出几件事,您需要防止 ActionLink 上的默认行为:

<script type="text/javascript">

        $(function () {
            $('#edit').click(function(event) {
                event.preventDefault();
                var name = $('#quantity').val();
                window.location = this.href + '&quantity=' + encodeURIComponent(name);
            });
        });

</script>

这应该将您的页面重定向到您想要的网址:localhost:port/WebProduct/Add?name=Name&type=Type&quantity=Quantity

如果您收到另一个错误,请检查您的控制器操作是否设置正确(拼写错误可能是个婊子)。

于 2012-11-29T07:23:35.783 回答