0

我将 XElement 传递给我的编辑视图,并希望为我构建常用的 HTML 控件并正确命名。我的代码是:

...
            <div class="editor-label">
                <%= Html.LabelFor(model => Model.XPathSelectElement("site[@id = 'customerName']").Value)%>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => Model.XPathSelectElement("site[@id = 'customerName']").Value)%>
                <%= Html.ValidationMessageFor(model => Model.XPathSelectElement("site[@id = 'customerName']").Value)%>
            </div>
...

但这会产生以下结果:

        <div class="editor-label"> 
            <label for="Value">Value</label> 
        </div> 
        <div class="editor-field"> 
            <input id="Value" name="Value" type="text" value="" />
        </div>

为什么会这样?如何让视图生成合理命名的(例如)文本框?

谢谢,

马特。

4

1 回答 1

0

您应该真正创建一个 ViewModel 来保存您的视图数据,而不是将完整的 xml 元素传递给它。

例如:

public class CustomerModel
{
  public CustomerModel(XElement xml)
  {
    this.Name = xml.XPathSelectElement("site[@id = 'customerName']").Value;
  }
  public string Name { get; set; }
}

然后简单地说: Html.TextBoxFor(model => model.Name);

编辑:还有......你的 <%= Html.LabelFor(model => Model .XPathSelectElement( 是 %= Html.LabelFor(model => model .XPathSelectElement(

于 2010-02-02T19:42:09.623 回答