我有一個索引視圖網格,我想在底部創建一個局部視圖 - 這樣用戶可以在同一頁面上創建一個新項目來查看所有項目。但是,在提交部分創建視圖時,主索引頁面沒有任何反應;它不會更新新項目(儘管它在刷新後會在那裏更新)。ASP.NET MVC:如何在索引視圖中創建一個部分視圖?
索引頁:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Paris.Domain.Models.PhoneNumberType>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<table>
<tr>
<th></th>
<th>
PhoneNumberTypeID
</th>
<th>
Name
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink("Edit", "Edit", new { id=item.PhoneNumberTypeID}) %> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.PhoneNumberTypeID })%>
</td>
<td>
<%: item.PhoneNumberTypeID %>
</td>
<td>
<%: item.Name %>
</td>
</tr>
<% } %>
</table>
<p>
Create New
</p>
<% Html.RenderAction("Create"); %>
</asp:Content>
這裏是控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Paris.Domain.Access;
namespace Paris.Web.Areas.Domain.Controllers
{
public class PhoneNumberTypesController : Controller
{
//
// GET: /Domain/PhoneNumberTypes/
private IRepository<Paris.Domain.Models.PhoneNumberType> db;
public PhoneNumberTypesController(IRepository<Paris.Domain.Models.PhoneNumberType> context)
{
db=context;
}
public ViewResult Index()
{
return View(db.Get().Select(i=>i));
}
public ViewResult IndexCreate(Paris.Domain.Models.PhoneNumberType num)
{
Create(num);
return View();
}
public ViewResult Edit()
{
return View();
}
[HttpPost]
public ViewResult Edit(Paris.Domain.Models.PhoneNumberType num)
{
try
{
if (ModelState.IsValid)
{
db.Update(num);
return Index();
}
else
return View();
}
catch
{
return View();
}
}
[HttpPost]
public ViewResult Delete(int PhoneNumberTypeID)
{
db.Delete(db.Get().First(i => i.PhoneNumberTypeID == PhoneNumberTypeID));
return Index();
}
[HttpPost]
public ViewResult Create(Paris.Domain.Models.PhoneNumberType num)
{
try
{
if (ModelState.IsValid)
{
db.Create(num);
return View();
}
else
return View();
}
catch
{
return View();
}
}
[HttpGet]
public ViewResult Create()
{
return View();
}
}
}
如果我這樣做完全錯誤的,隨意點出我的想法是怎樣的缺陷。
感謝
我剛剛發佈了它的代碼它有幫助 – Micah 2011-01-08 15:39:01