编辑
我把我的解决方案放在一个共享网站上。这样你就可以看到我在说什么了。你可以在这里下载:http ://www.easy-share.com/1909069597/TestRenderAction.zip
要对其进行测试,请启动项目(让创建表单为空)并单击创建按钮。你会看到会发生什么。
这只是一个示例项目。我现在不想将我的对象持久化到数据库中。我想让我的例子工作。
我得到了以下控制器:
public class ProductController : Controller
{
public ActionResult List()
{
IList<Product> products = new List<Product>();
products.Add(new Product() { Id = 1, Name = "A", Price = 22.3 });
products.Add(new Product() { Id = 2, Name = "B", Price = 11.4 });
products.Add(new Product() { Id = 3, Name = "C", Price = 26.5 });
products.Add(new Product() { Id = 4, Name = "D", Price = 45.0 });
products.Add(new Product() { Id = 5, Name = "E", Price = 87.79 });
return View(products);
}
public ViewResult Create()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Product product)
{
return View(product);
}
}
以下型号:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
我的产品/List.aspx:
<h2>List</h2>
<table>
<tr>
<th></th>
<th>
Id
</th>
<th>
Name
</th>
<th>
Price
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
<%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
</td>
<td>
<%= Html.Encode(item.Id) %>
</td>
<td>
<%= Html.Encode(item.Name) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:F}", item.Price)) %>
</td>
</tr>
<% } %>
</table>
<p>
<% Html.RenderAction("Create"); %>
</p>
我的产品/Create.ascx :
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm("Create", "Product", FormMethod.Post)) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Id">Id:</label>
<%= Html.TextBox("Id") %>
<%= Html.ValidationMessage("Id", "*") %>
</p>
<p>
<label for="Name">Name:</label>
<%= Html.TextBox("Name") %>
<%= Html.ValidationMessage("Name", "*") %>
</p>
<p>
<label for="Price">Price:</label>
<%= Html.TextBox("Price") %>
<%= Html.ValidationMessage("Price", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
问题是当我点击创建按钮并出现错误(如空字段)时,返回视图仅返回我的 Create.ascx 控件。它不会在我的 Create.ascx 控件中返回带有错误的 Product/List.asxp 页面。这就是我想要的。
知道如何解决这个问题吗?
我将 Asp.Net Mvc 1 与 Asp.Net Futures(具有 Html.RenderAction)一起使用。