我写了一个方法如下:
[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>
谢谢大家。