So I made a page for editing XML nodes but how exactly do I load the value from the node into the html.textboxfor
as I had been trying with
@Html.TextBoxFor(s => s.CarIsScrapped, new { @Value = CarIsScrapped}))
but then I get
CS0103: The name 'CarIsScrapped' does not exist in the current context
Now I can display or edit the nodes but can't do both as I either have to use
CarIsScrapped = node["CarIsScrapped"].InnerText = scrapped
for editing but then the textboxfor is empty
or CarIsScrapped = node["CarIsScrapped"].InnerText
for displaying but then I can't edit the node
my page
@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>
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);
}
}
The values that we are passing from TextBox will be loaded into Nodes in XML File (i.e., viceversa to point # 1) Previewing XML File after done with appropriate changes in Rich text box control. All we need to have is Visual Studio 2005 with VC# .NET and sample XML file.
The loadXmlSchema() function on line 30 loads an XML schema from the server’s file system using standard Node.js path and fs modules. Once again, we are using the parseXml() function to parse the schema file’s contents into a libxmljs.Document object. XML schemas are just XML documents at the end of the day.
According to the XML DOM, everything in an XML document is a node: The entire document is a document node Every XML element is an element node The text in the XML elements are text nodes
Particularly in business users perspective, XML has its own advantage. The values that we are passing from TextBox will be loaded into Nodes in XML File (i.e., viceversa to point # 1) Previewing XML File after done with appropriate changes in Rich text box control.
Do not use foreach, because this will cause problems when you try to bind your inputs back to the list of models. You need to use a loop in here:
for (var i = 0; i < Model.Count(); i++) {
@Html.TextBoxFor(m => Model[i].CarIsScrapped)
}
The question has been totally changed to a new question. This is answer to the new question.
Change the model of the page into List<SettingsModel>
. Then use a for
loop to create the text boxes for editing the model. Also for the Post
method, use a variable of type List<SettingsModel>
.
Here is the code that you need to use:
@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" />
}
There is a great article by Phil Haack about Model Binding to a List. Take a look at the article to learn more about editing non-sequential lists.
The question has been totally changed to a new question. This is answer to the old question.
@Html.TextBoxFor(s => s.CarIsScrapped, new { @Value = CarIsScrapped}))
obviously will result in CS0103: The name 'CarIsScrapped' does not exist in the current context exception because in the second parameter of the method (new { @Value = CarIsScrapped}
), the name CarIsScrapped
is not defined. In fact it's a property name and you can not use it directly.
In fact using TextBoxFor
, it's enough to use x => x.CarIsScrapped
and the second parameter is not needed.
Usually when you receive The name doesn't exist in the current context, you can check for these situations:
In this case you are using CarIsScrapped
like a variable and it seems this variable doesn't exists in current context.
The correct line of code should be:
@Html.TextBoxFor(s => s.CarIsScrapped)
And also, in the action, you need to pass the model to the page, unless you will see an empty text box.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With