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 == 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>

これにより、ページが目的の URL にリダイレクトされます。localhost:port/WebProduct/Add?name=Name&type=Type&quantity=Quantity

別のエラーが発生した場合は、コントローラー アクションが適切に設定されているかどうかを確認してください (スペル ミスは問題になる可能性があります)。

于 2012-11-29T07:23:35.783 に答える
0

この線

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

あなたのURLは次のようになります

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

このように 2 つの疑問符を同じ URL クエリ文字列に含めることはできません

更新: foreach ループにもあります。つまり、id 'quantity' を持つ複数のテキスト ボックスと、id 'edit' を持つ複数の追加リンクがあります。

更新 2: 'e.preventDefault()' を呼び出すために、クリック イベントにパラメータ 'e' を追加します。これにより、リンクがその URL に移動しなくなります。次に、this.href (リンク URL) ではなく、window.location も設定する必要があります。

    $(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 に答える