我已经开始做asp.net mvc编程,并且每天都更喜欢它。
我见过的大多数示例都使用单独的视图来查看和编辑特定实体的详细信息。
E.g。 - 链接到单独的“详细信息”和“更新”视图的音乐专辑表
使用mvc如何实现R和U(CRUD)在单个视图中表示的设计,此外用户可以编辑视图的不同部分,从而限制数据量用户可以在保存前进行编辑吗?
Example mockup - 编辑专辑detials:
我已经用ajax调用实现了这样的设计,但我很好奇如何在没有ajax的情况下做到这一点。 我自己的部分内容可以在下面看到。我用一个标志(enum EditCode) 指示必须呈现表单的视图的哪个部分(如果有)。 这样的设计是否符合框架,是否可以更优雅地完成?
AlbumController
public class AlbumController : Controller
{
public ActionResult Index()
{
var albumDetails = from ManageVM in state.AlbumState.ToList()
select ManageVM.Value.Detail;
return View(albumDetails);
}
public ActionResult Manage(int albumId, EditCode editCode)
{
(state.AlbumState[albumId] as ManageVM).EditCode = (EditCode)editCode;
ViewData["albumId"] = albumId;
return View(state.AlbumState[albumId]);
}
[HttpGet]
public ActionResult Edit(int albumId, EditCode editCode)
{
return RedirectToAction("Manage", new { albumId = albumId, editCode = editCode });
}
// edit album details
[HttpPost]
public ActionResult EditDetail(int albumId, Detail details)
{
(state.AlbumState[albumId] as ManageVM).Detail = details;
return RedirectToAction("Manage", new { albumId = albumId, editCode = EditCode.NoEdit });// zero being standard
}
// edit album thought
[HttpPost]
public ActionResult EditThoughts(int albumId, List<Thought> thoughts)
{
(state.AlbumState[albumId] as ManageVM).Thoughts = thoughts;
return RedirectToAction("Manage", new { albumId = albumId, editCode = EditCode.NoEdit });// zero being standard
}
标志 - EditCode
public enum EditCode
{
NoEdit,
Details,
Genres,
Thoughts
}
Mangae视图
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Controllers.ManageVM>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Manage
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Manage</h2>
<% if(Model.EditCode == MvcApplication1.Controllers.EditCode.Details)
{%>
<% Html.RenderPartial("_EditDetails", Model.Detail); %>
<% }else{%>
<% Html.RenderPartial("_ShowDetails", Model.Detail); %>
<% } %>
<hr />
<% if(Model.EditCode == MvcApplication1.Controllers.EditCode.Thoughts)
{%>
<% Html.RenderPartial("_EditThoughts", Model.Thoughts); %>
<% }else{%>
<% Html.RenderPartial("_ShowThoughts", Model.Thoughts); %>
<% } %>
答案 0 :(得分:0)
最后一部分对我来说很麻烦。我建议用Html Helpers包装那些来清理你的视图。
<h2>Manage</h2>
<% Html.RenderDetailsPartial(Model.EditCode) %>
<hr />
<% Html.RenderThoughtsPartial(Model.EditCode) %>
让HTMLHelper根据EditCode确定要使用的视图。