2

以下のようなメソッドを書きました。

[HttpPost]
    public ActionResult Create([Bind(Exclude = "ProductID")]Product product)
    {
        try
        {
            db.Products.AddObject(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

mvc2 で Create.aspx ページが正常に作成された後。

アプリケーションを実行します。その後、[新しいリンクの作成] をクリックすると、

ID付きのcreate.aspxも開きます。

マイページからIDを削除する方法を教えてください。

取得方法は次のとおりです。

public ActionResult Create()

    {

        return View();

    } 

私のHTMLページは以下のようなものです:

作成

<% using (Html.BeginForm()) {%>

    <%: Html.ValidationSummary(true) %>

    <fieldset>

        <legend>Fields</legend>

        <div class="editor-label">

            <%: Html.LabelFor(model => model.ProductID) %>

        </div>

        <div class="editor-field">

            <%: Html.TextBoxFor(model => model.ProductID) %>

            <%: Html.ValidationMessageFor(model => model.ProductID) %>

        </div>

        <div class="editor-label">

            <%: Html.LabelFor(model => model.ProductName) %>

        </div>

        <div class="editor-field">

            <%: Html.TextBoxFor(model => model.ProductName) %>

            <%: Html.ValidationMessageFor(model => model.ProductName) %>

        </div>

        <div class="editor-label">

            <%: Html.LabelFor(model => model.Cost) %>

        </div>

        <div class="editor-field">

            <%: Html.TextBoxFor(model => model.Cost) %>

            <%: Html.ValidationMessageFor(model => model.Cost) %>

        </div>

        <div class="editor-label">

            <%: Html.LabelFor(model => model.Quantity) %>

        </div>

        <div class="editor-field">

            <%: Html.TextBoxFor(model => model.Quantity) %>

            <%: Html.ValidationMessageFor(model => model.Quantity) %>

        </div>

        <p>

            <input type="submit" value="Create" />

        </p>

    </fieldset>


<% } %>

<div>

    <%: Html.ActionLink("Back to List", "Index") %>

</div>

ありがとうございます。

4

2 に答える 2

0

この属性をモデル プロパティに追加してみてください。これにより、フロントエンドで表示されなくなります。

 [HiddenInput(DisplayValue = false)]
于 2013-03-07T13:38:55.997 に答える
0

ビューから id を削除するにはどうすればよいですか? 単に。ビューでその宣言を削除するだけです。しかし、それは本当にあなたが望むものですか?

たとえば、更新シナリオでは ID が必要になります。したがって、本当に Id を削除したい場合は、モデルでそれを行う必要があります。

product.ProductId = null;
于 2013-03-07T09:46:16.947 に答える