gpt4 book ai didi

c# - 如何将 xml 节点加载到 html 文本框中

转载 作者:数据小太阳 更新时间:2023-10-29 01:52:14 24 4
gpt4 key购买 nike

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

因为我一直在尝试

@Html.TextBoxFor(s => s.CarIsScrapped, new { @Value = CarIsScrapped}))

但后来我明白了

CS0103: The name 'CarIsScrapped' does not exist in the current context

现在我可以显示或编辑节点,但不能同时使用,因为我必须使用

CarIsScrapped = node["CarIsScrapped"].InnerText = scrapped 

用于编辑,但随后文本框为空

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>

Controller

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);
}
}

最佳答案

不要使用 foreach,因为这会在您尝试将输入绑定(bind)回模型列表时导致问题。您需要在此处使用循环:

for (var i = 0; i < Model.Count(); i++) {
@Html.TextBoxFor(m => Model[i].CarIsScrapped)
}

关于c# - 如何将 xml 节点加载到 html 文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49534683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com