Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load xml node into html textboxfor

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>&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);
    }
}
like image 305
Kewin Björk Nielsen Avatar asked Mar 28 '18 12:03

Kewin Björk Nielsen


People also ask

How to pass values from textbox to nodes in XML file?

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.

How do I load an XML schema in Node JS?

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.

What is a node in XML?

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

What are the advantages of XML over rich text boxes?

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.


Video Answer


2 Answers

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)
}
like image 81
nguyenhoai890 Avatar answered Nov 01 '22 10:11

nguyenhoai890


Model binding to a List

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 name 'XXXX' does not exist in the current context

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:

  • You haven't defined a variable with that name.
  • You misspelled the variable name.
  • The namespace that defines a class is missing.
  • Your project needs to add a reference to the dll contains that type.

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.

like image 27
Reza Aghaei Avatar answered Nov 01 '22 11:11

Reza Aghaei