【问题标题】:object name is bind as prefix in input control对象名称在输入控件中绑定为前缀
【发布时间】:2016-05-08 15:09:51
【问题描述】:

目前我在 asp.net mvc 视图中工作

我使用以下代码创建表单。

@{
    T4.Models.Order o = new T4.Models.Order();
}

@using (Html.BeginForm("Create", "Default5", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Order</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
           @Html.LabelFor(model => o.CustomerID, new {@class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model=>o.CustomerID)
                @Html.ValidationMessageFor(model => o.CustomerID)
            </div>
        </div>
   </div>
}

此代码完美呈现,但问题是它使用对象名称的前缀呈现。

在下面显示 html 代码

<input class="text-box single-line" data-val="true" data-val-required="The CustomerID field is required." id="o_CustomerID" name="o.CustomerID" type="text" value="">

你可以看到这个 html 输入控件有 id="o_CustomerID"。我希望它只用 id="CustomerID" 呈现。

我不知道为什么它以对象名作为前缀。

请帮帮我。

谢谢。

【问题讨论】:

  • 您认为的模型是什么?不清楚你想在这里做什么。如果你想要一个表单来编辑Order,那么你的视图需要是@model Order

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor razorengine


【解决方案1】:

无需初始化 T4.Models.Order 类的对象。

@{
    T4.Models.Order o = new T4.Models.Order();
}

将上面的代码替换为: @model T4.Models.Order

然后简单使用:

<div class="form-group">
           @Html.LabelFor(model => model.CustomerID, new {@class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model=> model.CustomerID)
                @Html.ValidationMessageFor(model => model.CustomerID)
            </div>
        </div>

【讨论】:

  • 我必须创建对象,因为我的模型类已经用 @@model IEnumerable 进行了装饰
  • 如果你的模型已经用你的 IEnumerator 类装饰了,那么直接使用它。
  • 请告诉我如何使用 IEnumerator
  • 我可以告诉你一个小例子 IEnumerable
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多