【发布时间】:2018-03-28 12:34:26
【问题描述】:
所以我制作了一个用于编辑 XML 节点的页面,但是我究竟如何将节点中的值加载到 html.textboxfor
正如我一直在尝试的那样
@Html.TextBoxFor(s => s.CarIsScrapped, new { @Value = CarIsScrapped}))
然后我明白了
CS0103:当前上下文中不存在名称“CarIsScrapped”
现在我可以显示或编辑节点,但不能同时使用,因为我要么必须使用
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> </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>
控制器
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);
}
}
【问题讨论】:
-
@Html.TextBoxFor(s => s.CarIsScrapped, new { @Value = CarIsScrapped}))将导致 CS0103: The name 'CarIsScrapped' does not exist in the current context 异常 →CarIsScrapped是一个属性名称,你不能使用它直接。 -
我可以用 s => s.CarIsScrapped 更改值,具体怎么做