ASP.NET MVC:如何在索引视图中放置创建局部视图?

时间:2011-01-08 04:10:18

标签: 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();
        }
    }
}

如果我这样做完全错了,请随时指出我的想法是多么有缺陷。

由于

2 个答案:

答案 0 :(得分:1)

Create操作方法中,您只返回“创建”视图,而不是整个页面。这是因为View()的默认行为是返回一个与action方法同名的视图。因此,如果您希望Post返回整个页面,您需要指定正确的视图名称,或者甚至更好地返回重定向到Index操作。例如:return RedirectToAction("Index");

答案 1 :(得分:0)

从逻辑上讲,你所做的是对的。但是,在部分创建的提交按钮上,您的整个页面应该刷新。为什么不发生?您使用Ajax创建新记录吗?

我想这就是你在做的事情:

  1. 发布以创建操作。
  2. 创建记录,然后从数据源获取新记录集。
  3. 您将其传递回您的观点(return View("Index", recordset))。
  4. 使用新记录集刷新索引视图。
  5. 如果您正在执行这些步骤,那么它应该可以正常工作。

相关问题