【问题标题】:Model Binding MVC3 Razor, How do I change input name?模型绑定 MVC3 Razor,如何更改输入名称?
【发布时间】: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


    【解决方案1】:

    不确定“我已经设置了基本模型绑定器”是什么意思。它看起来不像模型绑定器,它看起来像模板或局部视图。

    不管怎样,继续……

    你应该有一个像这样的 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" />
    

    你想要什么,对吧?

    【讨论】:

    • 这就是我想要的,但是当我尝试您的解决方案时,我得到:传递到字典中的模型项的类型为“System.Data.Objects.ObjectQuery`1[BluesNetwork.Models.Track]” ,但此词典需要“BluesNetwork.Models.Track”类型的模型项。
    • @dreadlocks1221 - 显示返回此视图的控制器操作方法的代码。当您应该首先实现查询时,您似乎返回了 EF 查询,例如 var model = new TracksViewModel { Tracks = db.Tracks.ToList() }; return View(model);。您可能还应该为 Track 使用 ViewModel,但一次一步。
    • @dreadlocks1221 - 是的,我猜trackRepository.GetAssociatedTracks(id) 返回IQueryableIEnumerable。你需要在你的控制器中触发查询,就像我上面说的那样。
    • 那么为什么轨道视图模型不需要列表而不是 Ienumerable?
    • @dreadlocks1221 - List&lt;T&gt; 实现 IEnumerable&lt;T&gt;。你有没有试过我说的?
    【解决方案2】:

    基本上,如果您想更改名称,则需要破坏一些 JavaScript 或滚动您自己的局部视图。

    请注意,更改绑定字段的 id 和名称通常是一个糟糕的主意,因为这会破坏 ViewModel 的绑定绑定。真的需要改名吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 2012-06-11
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多