如何将xml节点加载到html textboxfor中

Kew*_*sen 6 c# xml asp.net razor

所以我创建了一个用于编辑XML节点的页面,但是我究竟如何将节点中的值加载到html.textboxfor中

正如我一直在努力的那样

@Html.TextBoxFor(s => s.CarIsScrapped, new { @Value = CarIsScrapped}))
Run Code Online (Sandbox Code Playgroud)

但后来我明白了

CS0103:当前上下文中不存在名称"CarIsScrapped"

现在我可以显示或编辑节点但不能同时使用这两个节点

CarIsScrapped = node["CarIsScrapped"].InnerText = scrapped 
Run Code Online (Sandbox Code Playgroud)

进行编辑但文本框为空

要么 CarIsScrapped = node["CarIsScrapped"].InnerText

用于显示但我无法编辑节点

我的页面

@using ActionLink_Send_Model_MVC.Models
@model IEnumerable<SettingsModel>

@{
    Layout = null;
}
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
    foreach (SettingsModel setting in Model)
    {

        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center"></th>
            </tr>
            <tr>
                <td class="auto-style1">Name: </td>
                <td class="auto-style1">
                    @Html.TextBoxFor(m => setting.CarIsScrapped)
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    @Html.ActionLink("Submit", "", null, new { @id = "submit" })</td>
            </tr>
            <tr>

            </tr>
            <tr>

            </tr>
            <tr>

            </tr>
        </table>
    }
    }
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#submit").click(function () {
                document.forms[0].submit();
                return false;
            });
        });
    </script>
</body>
Run Code Online (Sandbox Code Playgroud)

调节器

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index(SettingsModel setting)
    {
        List<SettingsModel> settings = new List<SettingsModel>();

        string scrapped = setting.CarIsScrapped;

        //Load the XML file in XmlDocument.
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/XML/Settings.xml"));

        //Loop through the selected Nodes.
        foreach (XmlNode node in doc.SelectNodes("Settings/UserSettings"))
        {
            //Fetch the Node values and assign it to Model.
            settings.Add(new SettingsModel
            {
                CarIsScrapped = node["CarIsScrapped"].InnerText = scrapped
            });
            doc.Save(Server.MapPath("~/XML/Settings.xml"));
        }
        return View(settings);
    }
}
Run Code Online (Sandbox Code Playgroud)

Rez*_*aei 1

模型绑定到列表

这个问题已经完全变成了一个新问题。这是对新问题的回答。

将页面的模型更改为List<SettingsModel>. 然后使用for循环创建用于编辑模型的文本框。另外,对于该Post方法,请使用 类型的变量List<SettingsModel>

这是您需要使用的代码:

@model List<SettingsModel>

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        <div>
            @Html.TextBoxFor(x => Model[i].CarIsScrapped)
        </div>
    }
    <input type="submit" value="Save" />
}
Run Code Online (Sandbox Code Playgroud)

Phil Haack有一篇关于模型绑定到列表的精彩文章。查看文章以了解有关编辑非顺序列表的更多信息。

当前上下文中不存在名称“XXXX”

这个问题已经完全变成了一个新问题。这是对老问题的回答。

@Html.TextBoxFor(s => s.CarIsScrapped, new { @Value = CarIsScrapped}))显然会导致CS0103: The name 'CarIsScrapped' 在当前上下文中不存在异常,因为在方法( new { @Value = CarIsScrapped}) 的第二个参数中,名称CarIsScrapped )的第二个参数中,未定义事实上它是一个属性名称,你不能直接使用它。

其实用TextBoxFor,用就够了x => x.CarIsScrapped不需要第二个参数。

通常当你收到 The name does not believe in the current context 时,你可以检查是否有这些情况:

  • 您尚未定义具有该名称的变量。
  • 您拼错了变量名称。
  • 缺少定义类的命名空间。
  • 您的项目需要添加对包含该类型的 dll 的引用。

在这种情况下,您正在使用CarIsScrapped变量一样,并且该变量似乎在当前上下文中不存在。

正确的代码行应该是:

@Html.TextBoxFor(s => s.CarIsScrapped)
Run Code Online (Sandbox Code Playgroud)

此外,在操作中,您需要将模型传递到页面,除非您会看到一个空文本框。