【发布时间】:2012-03-08 01:10:35
【问题描述】:
我通过将列表传递给视图并运行来设置基本模型绑定器:
控制器:
[Authorize]
public ActionResult AddTracks(int id)
{
TrackRepository trackRepository = new TrackRepository();
//ShowTrackAssociationHelper showTrack = new ShowTrackAssociationHelper();
//showTrack.tracks = trackRepository.GetAssociatedTracks(id).ToList();
//showTrack.show = showRepository.GetShow(id);
TracksViewModel tracksModel = new TracksViewModel();
tracksModel.Tracks = trackRepository.GetAssociatedTracks(id);
ViewBag.ShowID = id;
return View(tracksModel);
}
查看:
@model BluesNetwork.Models.TracksViewModel
@Html.EditorFor(model => model.Tracks, "TrackEditor")
TracksView 模型:
public class TracksViewModel
{
public IEnumerable<Track> Tracks { get; set; }
}
TackEditor:
@model BluesNetwork.Models.Track
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.TrackID)
@Html.HiddenFor(model => model.ShowID)
<div class="editor-label">
@Html.LabelFor(x => x.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TrackNumber)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.TrackNumber, new { maxlength = 2 })
@Html.ValidationMessageFor(model => model.TrackNumber)
</div>
@Html.HiddenFor(model => model.HQFileID)
@Html.HiddenFor(model => model.LQFileID)
<div class="editor-label">
@Html.LabelFor(model => model.InternalRating)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.InternalRating)
@Html.ValidationMessageFor(model => model.InternalRating)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.License)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.License)
@Html.ValidationMessageFor(model => model.License)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LicenseNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LicenseNumber)
@Html.ValidationMessageFor(model => model.LicenseNumber)
</div>
<input type="submit" value="Save" />
}
起初我得到的是:
这给了我每个输入的输出:
name="[0].ShowID"
我希望它是这样的:
name="track[0].ShowID"
我看到过这样的输出示例/教程,但他们没有详细说明。
在遵循 RPM1984 的建议并进行更改后,我得到了错误:
传入字典的模型项的类型为“System.Data.Objects.ObjectQuery`1[BluesNetwork.Models.Track]”,但此字典需要“BluesNetwork.Models.Track”类型的模型项。
提前感谢您的所有帮助
在
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-mvc-3 razor model-binding