【发布时间】:2011-12-02 16:24:57
【问题描述】:
我是 MVC 的新手,所以如果我这么菜鸟,请原谅我 :)。
在我的新项目中,我想制作一个“创建歌曲”页面,当然,它会创建一首新歌曲。在模型设计中,一首歌可以有很多艺术家。因此,在创建歌曲页面上,用户应该能够通过在搜索结果中点击艺术家的“添加”链接来搜索艺术家并从搜索结果中添加艺术家。
目前,我在表单中有两个提交按钮,分别是“提交”和“SearchArtist”,它们调用 http POST“创建”操作方法。 ViewModel 对象(CreateSongScrnData) 用于发布屏幕数据。一切正常。但是,当我尝试从 Artist 搜索结果中添加艺术家时,使用 @Url.Action 将 ViewModel 数据传递给“Create”http get 方法时,viewmodel 对象参数在进入 actiona 方法时始终为空。如果我遗漏了一些概念,请帮助我提供一些建议并启发我。非常感谢您。
代码如下。
public ActionResult Create(CreateSongScrnData modelData, Int32? artistIDToAdd)
{
if (modelData != null && artistIDToAdd != null)
{
var artist = db.Artists.Find(artistIDToAdd);
modelData.Song.Artists.Add(artist);
}
ViewBag.GenreID = new SelectList(db.Genres, "GenreID", "Name");
ViewBag.AlbumID = new SelectList(db.Albums, "AlbumID", "Name");
return View(modelData);
}
[HttpPost]
public ActionResult Create(CreateSongScrnData modelData)
{
switch (modelData.SubmitCommand)
{
case "AddNewSong":
if (ModelState.IsValid)
{
db.Songs.Add(modelData.Song);
db.SaveChanges();
return RedirectToAction("Index");
}
break;
default:
modelData.SearchResult = db.Artists.Where(a => a.Name.Contains(modelData.ArtistSearchString)).ToList();
break;
}
ViewBag.GenreID = new SelectList(db.Genres, "GenreID", "Name", modelData.Song.GenreID);
ViewBag.AlbumID = new SelectList(db.Albums, "AlbumID", "Name", modelData.Song.AlbumID);
return View(modelData);
}
视图中搜索的代码sn-p如下:
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Song.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Song.Name)
@Html.ValidationMessageFor(model => model.Song.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Song.MyanmarName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Song.MyanmarName)
@Html.ValidationMessageFor(model => model.Song.MyanmarName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Song.Genre)
</div>
<div>
@Html.DropDownList("GenreID", "Choose Genre")
@Html.ValidationMessageFor(model => model.Song.GenreID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Song.AlbumID, "Album")
</div>
<div class="editor-field">
@Html.DropDownList("AlbumID", "Choose Album")
@Html.ValidationMessageFor(model => model.Song.AlbumID)
</div>
<div class="editor-label">
<p>Sing by this artists:</p>
@if (Model.AddedArtists != null)
{
<table><tr><th>Name</th></tr>
@foreach (var item in Model.AddedArtists)
{
<tr><td>@item.Name</td></tr>
}
</table>
}
</div>
<div style=" border-top:1px solid #ccc; width: 400px; height:auto; margin-top:30px; margin-bottom:30px;"></div>
<div>
<p>Search and add artists</p>
@Html.TextBoxFor(model => model.ArtistSearchString)
<input type="submit" name="SubmitCommand" value="SearchArtist" class="cancel" />
@if (Model.SearchResult != null)
{
<div>
<table><tr><th>Name</th><th></th></tr>
@foreach (var item in Model.SearchResult)
{
<tr><td>@item.Name</td>
<td><a href="@Url.Action("Create", "Song", new { modelData = Model })">Add</a></td>
</tr>
}
</table>
</div>
}
</div>
<div style=" border-top:1px solid #ccc; width: 400px; height:auto; margin-top:30px; margin-bottom:30px;"></div>
<p>
<input type="submit" name="SubmitCommand" value="AddNewSong" />
</p>
}
【问题讨论】:
-
我在你的视图中只看到一个提交按钮。
-
您好 Dismissile,这只是表单的“搜索艺术家”部分,我只是添加了创建页面的完整代码。请看一看。谢谢。 :)
标签: asp.net-mvc forms asp.net-mvc-3