0

このようなビューで呼び出されるボタンがあります

<form>
    <div class="field">
        <label for="product-name">Product Name</label><br />
        @Html.TextBoxFor(x=> x.Name, new {id="product-name",required="required"}) <br />
        <span class="helper">Give your product a name.</span>
        @ViewBag.mm
    </div>
    <div class="field">
        <label for="product-variety">Product Variety</label><br />
        <input id="product-variety" type="text" /><br />
        <span class="helper">Optionally, give your product a variety.</span>
    </div>
    <div class="field">
        <label for="product-description">Description</label><br />
        <input id="product-description" type="text" /><br />
        <span class="helper">Add an optional description to help identify your product.</span>
    </div>
    <div class="field">
        <label for="product-comments">Comments</label><br />
        <textarea id="product-comments"></textarea><br />
        <span class="helper">Add any further information or comments relating to this product.</span>
    </div>
    <input id="product-save-return" type="submit" value="Add Product and Return to Products" class="green-button" /> <input id="product-save-associate" type="button" value="Add Product and Associate with Sites" class="green-button" /> <input id="file-cancel" type="button" value="Cancel" class="green-button" />

コントローラには、このような機能があります

[HttpPost]
public ActionResult Add(Product product)
{
    SupplierContext db = new SupplierContext();
    Response.Write("Inside");
    ViewBag.mm = "xyz";
    if (ModelState.IsValid)
    {
        product.Key = Guid.NewGuid();
        product.Type = ProductType.Chemical;
        product.Status = EntityStatus.Default;
        db.Products.Add(product);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(product);
}

しかし、ボタンをクリックすると、コントローラーの機能が動作しません。

4

2 に答える 2

1

ボタンはフォーム/アクション内にありますか? Razor では、次の構文でフォーム要素を追加するだけです。

@using (Html.BeginForm())
{
    <div class="field">
        <label for="product-name">Product Name</label><br />
        @Html.TextBoxFor(x=> x.Name, new {id="product-name",required="required"}) <br />
        <span class="helper">Give your product a name.</span>
        @ViewBag.mm
    </div>
    <div class="field">
        <label for="product-variety">Product Variety</label><br />
        <input id="product-variety" type="text" /><br />
        <span class="helper">Optionally, give your product a variety.</span>
    </div>
    <div class="field">
        <label for="product-description">Description</label><br />
        <input id="product-description" type="text" /><br />
        <span class="helper">Add an optional description to help identify your product.</span>
    </div>
    <div class="field">
        <label for="product-comments">Comments</label><br />
        <textarea id="product-comments"></textarea><br />
        <span class="helper">Add any further information or comments relating to this product.</span>
    </div>
    <input id="product-save-associate" type="button" value="Add Product and Associate with Sites" class="green-button" />  <input id="file-cancel" type="button" value="Cancel" class="green-button" />
   <input id="product-save-return" type="submit" value="Add Product and Return to Products" class="green-button" />
}

これが、コントローラーで機能を取得していない理由である可能性があります。AJAX投稿を使用して、JavaScriptでもこれを行うことができます...

于 2013-01-23T09:23:18.413 に答える
0

ActionLink を使用することもできます。

@Html.ActionLink("Add Product and Return to Products", "Add", "YourController")

これはボタンではなくリンクになりますが。

于 2013-01-23T09:26:24.860 に答える