我這樣做。我不知道這是否是最佳做法,但它可以很好。有些情況下完全獨立的添加/編輯視圖可能會有用。此外,如果您使用ViewModels,那麼據我所知,您使用相同的ViewModel進行添加和編輯。理論上他們都應該擁有自己的ViewModels。
以下是這看起來對我來說:
AddVideo.cshtml
@model Multimedia.MediaVideoViewModel
@{
Layout = "~/Views/Shared/LiveSubLayout.cshtml";
}
@section AdditionalHeadContent {
}
<div class="page-header">
<h1>Add a new video</h1>
</div>
<div id="add-video" class="row-fluid">
@Html.Partial("_VideoForm", Model, new ViewDataDictionary { { "ActionKeyword", "Add" } })
</div>
EditVideo.cshtml
@model Multimedia.MediaVideoViewModel
@{
Layout = "~/Views/Shared/LiveSubLayout.cshtml";
}
@section AdditionalHeadContent {
}
@if (ViewBag.Success)
{
<div class="alert alert-success">
<button class="close" data-dismiss="alert">×</button>
<h3><strong>Video saved!</strong></h3><br/>
<div class="btn-group">
<a href="#" class="btn">Preview this video</a>
@Html.ActionLink("Add Another Video", "AddVideo", "Multimedia", new { Model.Id }, new { @class = "btn" })
@Html.ActionLink("View all media", "Index", "Multimedia", null, new { @class = "btn" })
</div>
<p>or continue editing below...</p>
</div>
}
<div class="page-header">
<h1>Edit video <small>@Model.Title</small></h1>
</div>
<div id="edit-video" class="row-fluid">
@Html.Partial("_VideoForm", Model, new ViewDataDictionary { { "ActionKeyword", "Edit" } })
</div>
_VideoForm.cshtml(部分)
@model Multimedia.MediaVideoViewModel
@{
string actionKeyword = ViewData["ActionKeyword"].ToString();
}
<div class="span6">
@using (Html.BeginForm("editvideo", "multimedia"))
{
<label class="control-label" id="embed-url">Paste video URL here:</label>
<div class="control-group">
@Html.TextBoxFor(model => model.EmbedUrl, new { @class = "span12", id = "video-url", placeholder = "ex: http://www.youtube.com/watch?v=PoAGasPLh30" })
<button class="btn disabled" id="get-video" title="Tooltip">Get Video</button>
</div>
<div class="video-meta">
<h3>Video Information</h3>
<label class="control-label">Title:</label>
<div class="control-group">
@Html.TextBoxFor(model => model.Title, new { @class = "span12", id = "video-title" })
@Html.ValidationMessageFor(model => model.Title, "A title is required", new { @class = "label label-important" })
</div>
<label class="control-label">Description:</label>
<div class="control-group">
@Html.TextAreaFor(model => model.Description, new { @class = "span12", id = "video-description" })
</div>
<h3>Categories</h3>
<div id="tag-search" class="well">
<label class="control-label">Search tags:</label>
<div class="controls"><input type="text" class="typeahead" /></div>
@if (Model != null)
{
foreach (var category in Model.Tags)
{
@Html.Partial("_TagFragment", category)
}
}
</div>
<hr />
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.ThumbnailUrl, new { id = "thumb-url" })
<input type="submit" id="video-submit" name="video-submit" class="btn-large btn-primary" value="@actionKeyword video" />
</div>
}
</div>
我編輯了這些,所以可能會丟失一些東西,但這應該給你一個總體思路。
可能的[ASP.NET MVC的重複 - 使用相同的形式來創建和編輯](http://stackoverflow.com/questions/399914/asp-net-mvc-using-the-same-form-to -both-create-and-edit) – 2014-12-03 15:27:56