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。

获取方法是:

公共动作结果创建()

    {

        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 ?简单地。只需在 View 上删除它的声明。但这真的是你想要的吗?

例如,对于更新方案,您将需要 Id。所以如果你真的想删除 Id 你应该在你的模型上做,所以:

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