Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 传递XElement时如何在MVC中命名文本框_C#_Xml_Model View Controller_Xelement - Fatal编程技术网

C# 传递XElement时如何在MVC中命名文本框

C# 传递XElement时如何在MVC中命名文本框,c#,xml,model-view-controller,xelement,C#,Xml,Model View Controller,Xelement,我正在向我的编辑视图传递一个XElement,希望为我构建一个普通的HTML控件并正确命名。我的密码是: ... <div class="editor-label"> <%= Html.LabelFor(model => Model.XPathSelectElement("site[@id = 'customerName']").Value)%> </div>

我正在向我的编辑视图传递一个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>
...
。。。
Model.XPathSelectElement(“site[@id='customerName']”).Value)%>
Model.XPathSelectElement(“site[@id='customerName']”).Value)%>
Model.XPathSelectElement(“site[@id='customerName']”).Value)%>
...
但这会产生以下结果:

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

价值
这是为什么?我如何让视图生成合理命名的(eg)文本框

谢谢


Matt。

您应该真正创建一个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)

编辑:还有。。。您的模型不应该.XPathSelectElement吗(
be%=Html.LabelFor(model=>model.XPathSelectElement(

谢谢。我希望有一种方法可以设置文本输入元素的ID,但我会这样做。是的,你是对的,尽管我得到的输出与我设置lambda表达式的结果相同。