我已经通过将列表传递给视图并运行:
来设置基本模型绑定器控制器:
[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" />
}
起初我得到了:
这给了我每个输入的输出:
名= “[0] .ShowID”
然而我希望它是:
名称= “轨道[0] .ShowID”
我看过这样的示例/教程显示了这样的输出,但他们没有详细介绍它。
按照RPM1984的建议并进行更改后,我收到错误:
传递到字典中的模型项的类型为'System.Data.Objects.ObjectQuery`1 [BluesNetwork.Models.Track]',但此字典需要“BluesNetwork.Models.Track”类型的模型项。 / p>
提前感谢您的所有帮助
答案 0 :(得分:2)
不确定你的意思&#34;我已经设置了一个基本的模型绑定器&#34;。这看起来不像模型活页夹,看起来像模板或部分视图。
无论如何,继续......
你应该有一个像这样的ViewModel:
public class TracksViewModel
{
public IEnumerable<Track> Tracks { get; set; }
}
主要观点:
@model TracksViewModel
@Html.EditorFor(model => model.Tracks)
编辑模板:
@model Track
@Html.EditorFor(model => model.ShowId)
没有循环,没有神奇的字符串。好的。
将会像这样呈现HTML:
<input type="text" name="tracks[0].ShowId" />
<input type="text" name="tracks[1].ShowId" />
<input type="text" name="tracks[2].ShowId" />
你想要的是什么,对吧?
答案 1 :(得分:0)
基本上,如果您想更改名称,则需要删除一些JavaScript或滚动自己的局部视图。
请注意,更改ID和绑定字段的名称通常是个不好的主意,因为这会破坏ViewModel的绑定。你真的需要改名吗?